WordPress For Dummies
Book image
Explore Book Buy On Amazon

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:

About This Article

This article is from the book:

About the book author:

Lisa Sabin-Wilson is cofounder of WebDevStudios, one of the largest WordPress design and development agencies in the world. She is a regular public speaker at national events on topics such as WordPress, development, design, CSS, and social media.

This article can be found in the category: