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!