WordPress template parts are relatively new. A template part is very similar to the Header, Footer, and Sidebar templates except that you aren’t limited to just these. You can branch out and create any number of template parts to call into your WordPress theme to provide specific functions, such as displaying posts from a specific category or displaying a gallery of photos you’ve uploaded to your website.
The get_header, get_footer, and get_sidebar functions allow for code that was once duplicated in many of the template files to be placed in a single file and loaded using a standard process.
The purpose of template parts is to offer a new standardized function that can be used to load sections of code specific to an individual theme. Using the concept of template parts, sections of code that add a specialized section of header widgets or display a block of ads can be placed in individual files and easily loaded as a template part.
Template parts are loaded via the get_template_part function. The get_template_part function accepts two parameters:
Slug: The slug parameter is required and describes the generic type of template part to be loaded, such as content.
Name: The name parameter is optional and selects a specialized template part, such as post.
A call to get_template_part with just the slug parameter tries to load a template file with the slug.php filename. Thus, a call to get_template_part(‘content’) tries to load content.php. And a call to get_template_part('header’ ,‘widgets') tries to load header-widgets.php. See a pattern here? The term slug, refers to the name of the template file, minus the .php extension, because WordPress already assumes that it’s a PHP file.
A call to get_template_part with both the slug and name parameters tries to load a template file with a slug-name.php filename. If a template file with a slug-name.php filename doesn’t exist, WordPress tries to load a template file with a slug.php filename.
Thus, a call to get_template_part(‘content’, 'post') first tries to load content-post.php followed by content.php if content-post.php doesn’t exist. A call to get_template_part('header-widgets', 'post') first tries to load header-widgets-post.php followed by header-widgets.php if header-widgets-post.php doesn’t exist.
The Twenty Thirteen theme offers a good example of the template part feature in use; it uses a loop template part to allow The Loop to be pulled into individual template files.
The Loop is the section of code found in most theme template files that uses a PHP while loop to literally loop through the set of content (such as post, page, archive, and so on) and then display it. The presence of The Loop in a template file is crucial for a theme to function properly.
Twenty Thirteen’s index.php template file shows a template part for The Loop in action in line 7 of the following code using the get_template_part(); template tag:
<?php get_header(); ?> <div id="primary" class="content-area"> <div id="content" class="site-content" role="main"> <?php if ( have_posts() ) : ?> <?php /* The loop */ ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?> <?php twentythirteen_paging_nav(); ?> <?php else : ?> <?php get_template_part( 'content', 'none' ); ?> <?php endif; ?> </div><!-- #content → </div><!-- #primary → <?php get_sidebar(); ?> <?php get_footer(); ?>
Loading The Loop by using a template part, Twenty Thirteen cleans up the index.php code considerably when compared with other themes. This cleanup of the template file code is just the icing on the cake. The true benefits are the improvements to theme development.
Twenty Thirteen’s index.php template file calls for a template part with a slug of content and the get_post_format(); tag. The get_post_format(); refers to post formats that are in use in the theme. If you look at the other template files in Twenty Thirteen, you’ll see files with the content slug:
content-aside.php
content-audio.php
content-chat.php
content-gallery.php
content-image.php
content-link.php
content-none.php
content-quote.php
content-status.php
content-video.php
content.php
The get_post_format(); tag within the get_template_part(); automatically picks up the type of post format defined for the post and uses the corresponding template file. For example, if the post format is image, the content-image.php is used. Alternatively, if no post format is defined, WordPress simply uses content.php.
Before template parts, the full Loop code, which can have up to ten lines of code (or more), would be duplicated in each of the template files, over and over. This means that a modification to the index.php file’s Loop code would also require the same modification to the single.php file.
Imagine if you had to make the same modification to five template files; repeatedly making the same modifications would quickly become tiring, and each modification would increase your chance of making mistakes. Using a template part means that the modifications to The Loop need to be made only once, so it’s applied to all templates using The Loop code via the get_template_part(); function, cutting down your overall development time.
The get_template_part call allows for easily creating as many customized templates as needed without having to duplicate the multiple lines of The Loop code over and over again. Without the duplicate code, the code for The Loop can be easily modified in one place.
When you duplicate sections of code in numerous template files, place the code in a separate file and use the get_template_part function to load it where needed.