The function get_template_part
was introduced in WordPress 3.0 and has been widely used as a means to call the component parts of “the_Loop” as they relate to the WordPress core post-format; but, you do not have to stop there. This functionality can be extended beyond its commonly found implementations in “the_Loop”.
Let’s start by having a look at this bit of code:
<?php get_template_part( 'wpfa-loop', get_post_format() ); ?>
This will load the ‘wpfa-loop.php’ template part; if the loop is meant to display a specific post-format, for example a “quote” post-format, this will load the ‘wpfa-loop-quote.php’ template part, if it exists. The general idea is the file “{slug}-{name}.php” will be loaded where “{slug}” and “{name}” are used in the function call get_template_part({slug}, {name})
. See this WordPress Codex page for additional details.
This being the case, a sample ‘index.php’ template file that takes advantage of this functionality might look like the following:
<?php get_header( get_post_format() ); ?> <div class="content-wrapper"> <div class="the-loop"> <?php if ( have_posts() ): while ( have_posts() ): the_post(); get_template_part( 'wpfa-loop', get_post_format() ); endwhile; else: /** Add search results functionality */ endif; /** Add other functionality */ ?> </div><!-- .the-loop --> <?php get_sidebar( get_post_format() ); ?> </div><!-- #content-wrapper --> <?php get_footer( get_post_format() );
On a side note, calls to get_header
, get_sidebar
, and get_footer
are just special instances of the get_template_part
function. From the example above, these functions calls would respectively load ‘header-{name}.php’, ‘sidebar-{name}.php’, and ‘footer-{name}.php’.
Now, let’s do more …
… there is no reason why you cannot apply this same “loop” functionality to any of the standard “content” template files found in a WordPress theme. For example, lets look at the ‘archive.php’ template file and this code using the above template as a starting point:
<?php get_header( get_post_format() ); ?> <div class="content-wrapper"> <div class="the-loop"> <?php if ( have_posts() ): while ( have_posts() ): the_post(); get_template_part( 'archives/archive', get_post_format() ); endwhile; else: /** Add search results functionality */ endif; /** Add other functionality */ ?> </div><!-- .the-loop --> <?php get_sidebar( get_post_format() ); ?> </div><!-- #content-wrapper --> <?php get_footer( get_post_format() );
I highlighted line 09 in the code for emphasis … get_template_part( 'archives/archive', get_post_format() )
is where the magic begins. Note the “{slug}” is ‘archives/archive’; this is necessary as placing the ‘archive-{name}.php’ files in the root with the ‘archive.php’ file will create a recursive looping nightmare if a post-format type (read: {name}) specific template part does not exist … besides it’s easier to manage these files in their own folder.
Now, continuing from our example ideas above you will need to create a standard ‘archive.php’ loop template and a ‘archive-quote.php’ loop template; and, place them both in the ‘archives’ folder of your theme. How you present these archives I will leave as an exercise for you, the reader; think of them much like you would consider the standard loop templates for these formats.
To see this idea in action, have a look around; this site currently uses a Child-Theme of Opus Primus which implements this method in several places. For example, if you click on a post-format button you will be taken to that post-format’s archive. This is the same for any site currently running the Opus Primus Framework Theme … feel free to visit OpusPrimus.com or EdwardCaissie.com for more live examples.