How to Create and Use Template Parts in WordPress Twenty Fifteen
A template part is very similar to the Header, Footer, and Sidebar templates in WordPress 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. A call to get_template_part(‘header’ ,’widgets’) tries to load header-widgets.php. See a pattern here? A slug is simply 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 Fifteen 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 Fifteen’s index.php template file shows a template part for The Loop in action in line 3 of the following code using the get_template_part(); template tag:
<div id=“primary” class=“content-area”> <main id=“main” class=“site-main” role=“main”> <?php if ( have_posts() ) : ?> <?php if ( is_home() && ! is_front_page() ) : ?> <header> <h1 class=“page-title screen-reader-text”><?php single_post_title(); ?></h1> </header> <?php endif; ?> <?php // Start the loop. while ( have_posts() ) : the_post(); * Include the Post-Format-specific template for the content. * If you want to override this in a child theme, then include a file called content-___.php (where ___ is the Post Format name) and that will be used instead. */ get_template_part( ‘content’, get_post_format() ); // End the loop. endwhile; // Previous/next page navigation. the_posts_pagination( array( ‘prev_text’ => __( ‘Previous page’ ‘twentyfifteen’ ), ‘next_text’ => __( ‘Next page’, ‘twentyfifteen’ ), ‘before_page_number’ => ‘<span class=“meta-nav screen-reader-text”>‘ . __( ‘Page’, ‘twentyfifteen’ ) . ‘ </span>‘, ) ); // If no content, include the “No posts found” template. else : get_template_part( ‘content’, ‘none’ ); endif; ?> </main><!-- .site-main → </div><!-- .content-area →
Loading The Loop by using a template part, Twenty Fifteen 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.
In the previous code example, you see Twenty Fifteen’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 Fifteen, you’ll see files with the content slug:
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 link, the content-link.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.