Skip to content

Understanding get_template_part

Jim Reevior edited this page Aug 14, 2018 · 1 revision

Understanding get_template_part()

get_template_part() is a powerful function in WordPress that allows a theme to break its template files into smaller, reusable templates and parts. It also allows you to have multiple templates for the same content with different contexts.

The power of get_template_part() is that it is aware of the parent/child theme relationship and will look for multiple files that match the arguments provided in a specific order.

The function takes two arguments: a slug and a name (or context).

Example: Part 1

As an example, imagine we have a post type created called degree_program. To create a template part and display it, we would use the following code snippet:

<?php get_template_part( 'template-parts/degree_program' ); ?>

NOTE: Template parts should always be stored in the template-parts folder.

Using the snippet above, get_template_part() will look for the following template files in the following order:

  • child-theme/template-parts/degree_program.php
  • parent-theme/template-parts/degree_program.php

The function will always give priority to a template part in the child theme over the parent theme, but will always attempt to fall back to the parent theme.

When calling get_template_part(), a context argument should always be used to describe where the template part is being loaded.

<?php get_template_part( 'template-parts/degree_program', 'archive' ); ?>

This adds additional templates to the search in get_template_part():

  • child-theme/template-parts/degree_program-archive.php
  • parent-theme/template-parts/degree_program-archive.php
  • child-theme/template-parts/degree_program.php
  • parent-theme/template-parts/degree_program.php

In the theme, if we created a template-parts/degree_program-archive.php file, this would be used instead of template-parts/degree_program.php when the archive context is passed. If that file does not exist, it would just fallback to the default template part for that slug.

Example: Part 2

Now, say that the degree_program post type is listed on the home page, the degree program archive page, and on single degree programs in a related degrees section.

The designs for the post type archive and related degree programs on the single pages are the same, but the home page is different. How do we correctly use get_template_part() here?

On the home page, get_template_part() should be called with the home context, and a template-parts/degree_program-home.php file should be created with the markup for the home page.

<?php get_template_part( 'template-parts/degree_program', 'home' ); ?>

Now, there are two template files for this post type:

  • child-theme/template-parts/degree_program-home.php
  • child-theme/template-parts/degree_program.php

The first one will only be used when home is passed to the function. Any other value passed to the function will result in template-parts/degree_program.php being used.

Welcome to Responsive!

Get started

Configuration

Build child themes

Sass

Javascript

PHP

Shortcodes

Templates

GitHub

Tasks

Contribute to the framework

Code Examples

BU Developer Resources

Clone this wiki locally