Create and Use WordPress Template Parts
A WordPress template part is very similar to the Header, Footer, and Sidebar templates except 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 loop.
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('loop') tries to load loop.php. And a call to get_template_part('header' ,'widgets') tries to load header-widgets.php. 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('loop', 'post') first tries to load loop-post.php followed by loop.php if loop-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 Ten 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.
Twenty Ten's index.php template file shows a template part for The Loop in action:
<?php get_header(); ?> <div id="container"> <div id="content" role="main"> <?php get_template_part('loop', 'index'); ?> </div> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
Loading The Loop by using a template part, Twenty Ten cleans up the index.php code considerably when compared to other themes, but the true benefits are the improvements to theme development.
Twenty Ten's index.php template file calls for a template part with a slug of loop and an index. Because Twenty Ten doesn't supply a loop-index.php file, loop.php is used. This allows a child theme to supply a loop-index.php file to customize just The Loop for index.php.
A child theme can do this without supplying a customized index.php file because of Twenty Ten's good use of template parts and both parameters (slug and name) of the get_template_part function.
With Twenty Ten’s code for The Loop, header, sidebar, and footer placed into separate files, the template files become much easier to customize for specific uses. You can see the benefit of template parts by comparing the page.php and onecolumn-page.php template files. This is the page.php listing:
<?php get_header(); ?> <div id="container"> <div id="content" role="main"> <?php get_template_part('loop', 'page'); ?> </div> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
This is the onecolumn-page.php listing:
/* Template Name: One column, no sidebar */ <?php get_header(); ?> <div id="container" class=one-column> <div id="content" role="main"> <?php get_template_part('loop', 'page'); ?> </div> </div> <?php get_footer(); ?>
Other than onecolumn-page.php having the Template Name comment at the top, which allows it to be used as a Page template, the only differences are that page.php has the get_sidebar function call and onecolumn-page.php adds a one-column class to the container div. With just these modifications and a few styling rules added for the one-column class in the CSS (style.css), your theme has a Page template without a sidebar.
Before template parts, the full Loop code, which can have up to ten lines of code (or more), would be duplicated in the page.php and onecolumn-page.php files. This means that a modification to the page.php file's Loop code would also require the same modification to the onecolumn-page.php file.
Repeatedly making the same modifications would quickly become tiring, and each modification would increase the chance of making mistakes. Using a template part means that the modifications to the Loop needs to be made only once so it then is applied to all templates using the Loop code via the get_template_part(); function. This cuts down on your development time, overall.