Now that I’m working with custom post types a bit more on this site, for my plugin reviews and speaking agenda, for instance, I’m also starting to appreciate the power of the WordPress template part functionality a whole lot more, which was introduced in WordPress 3.0. Let me explain how I use it and how I think you should use it.
My whole loop for taxonomy archives, other archive pages and search results now just looks like this:
while ( have_posts() ) : the_post(); get_template_part( '/partials/excerpt' , $post->post_type ); endwhile;
As you can see, I get the post type for the current post and then use that to load the template part that will display that post type. For each different post type I have created a snippet that displays that post type in it’s own specific way, a plugin review would have a rating attached to it, a speaking event would be marked up as an hCalendar event and so forth.
Let’s say the post type is speaking_event. The above code would try to load, from my theme’s directory, the file /partials/excerpt-speaking_event.php. If that file doesn’t exist, it would load /partials/excert.php. So if I create a new custom post type, it will fall back to the “default” display of a post, which is in excerpt.php, and if I decide to give it another look, it will get it’s own “short” WordPress template part.
Of course, as this loop itself is referenced about 15 times on all the different forms of archive templates I have, it in itself is a template simply called ‘loop’. The best thing about this is that now, when you search for something that results in showing several post types, or you have a taxonomy attached to several post types, they’re all displayed in their own specific way. See an example of that below:
More WordPress template part ideas?
Now I’d love to know whether you are using a WordPress template part in your theme and if so, how and where, please share in the comments so we can build a “gallery” of WordPress template part ideas!




thanx for this. i love ur plugin! :)
I’ve not used template parts before but will definitely look into it.
Do you use post formats as well as custom post types, or do you see no value in them?
Don’t use post formats yet, haven’t found a use for them yet.
This is a great idea that I’ll be adding to our themes.
Currently we are using template part a lot on our parent themes. This way we can just redo a part in the child theme and not have to copy the entire section for a small change.
For example we typically call horizontal navigation from header.php but we place all the code for it inside a part-nav.php file.
I recently started using template parts in similar fashion.
Template parts have one more advantage worth a mention: each of them can be individually “overloaded” in a child theme. So, if for example you want to change the look of the speaking event excerpt in the child theme, then only this theme part needs to be copied and tweaked. No need to touch the main loop.
This probably doesn’t really apply to a theme developed for your own website, but in a theme intended for distribution this can be great help for users wanting to customise it.
This is great, I haven’t even begun to experiment so this is useful information!
Thanks Joost,
I will check-out this plugin. I am not using it yet but I have used some of your other plugins. Thanks for all of your hard work.
You should check your tags pages. I noticed while using your tag cloud that, while the pages load, no posts seem to appear on them.
Examples – http://yoast.com/tag/b2b-seo/ and http://yoast.com/tag/affiliate-marketing/
Yeh same here. Im running Chrome and i also do not see the tagged pages.
Yep, same here, and the load time is ~5 seconds.
Thanks for this, i really love it :)
I’ve recently started using template parts also and I love them. For me, I’m using them to make my themes highly modular and hence a breeze to customize.
For example, if there is a layout style used throughout my theme I have that extracted into its own template part. Then if I want to stack something else into that aspect of the theme layout, like adding a div with a round corner background on top of posts, I need only change one file and everything automatically updates.
I’m also finding it great for changing up the order of theme elements. For example, I have template parts for the post title, featured image, and top & bottom post meta info. This makes everything very neat and fast to alter.
Template parts essentially turn your themes into a big box of Lego and you can then easy assemble the pieces however you choose. I love it.
I’m using template parts to overwrite the standard TwentyTen theme. I need to list the posts
within pages and also modify the presentation of image galleries. As an example for the older
Wordpress you find a solution at the Codex
A Page of Posts. But this is not
the style of TwentyTen. So I replaced the whole have_posts() section in the example with the call of a customized
template by get_template_part( ‘loop’, ‘gallery’ ). This calls the template “loop-gallery.php”
which is a modified copy of the original “loop.php” from TwentyTen.
I’ve placed all modified files in a subtheme, so updates of the original theme will not interfere.
great work, best plugins ^^
very interesting – thanks