Home > Help Center > Applications > WordPress > WordPress Template Parts

WordPress Template Parts

When we need to create reusable components for WordPress templates, one of the most useful functions and features that we have at our disposable is the get_template_part() function.

The function should be used when we need to:

  • Load a template part into a template (other than header, sidebar, footer).
  • If a piece of code is going to be reused across multiple templates
  • When we want to keep our theme code DRY (it stands for Don't Repeat Yourself)

The get_template_part() function allows you to break your theme down into smaller templates (or template parts), which can be reused across your other templates.

Although get_template_part() is similar to a PHP include() or require(), you should not use it to include theme options code, sidebars registration, custom widgets, etc.

The get_template_part() function should only be used to get template parts.

Function description

get_template_part( string $slug, string $name = null )

Includes the named template part for a theme or if a name is specified then a specialised part will be included. If the theme contains no {slug}.php file then no template will be included.

For the $name parameter, if the file is called {slug}-special.php then specify special.

Parameters

$slug - (string) (Required) The slug name for the generic template.

$name - (string) (Optional) The name of the specialised template.
Default value: null

Practical Usage Examples

Suppose your template is build of several blocks - breadcrumbs, content and advertising.

By using the get_template_part() function our template will contain the following code:

get_template_part( 'content', 'breadcrumbs'); get_template_part( 'content', 'post'); get_template_part( 'content', 'ads');

Following the established naming conventions WordPress will look for these files:

content-breadcrumbs.php content-post.php content-ads.php

If any of these files do not exist, the WordPress will fall back to content.php template.

Doing this we can create all needed libraries of code and include them wherever we need it.

Splitting the code into small individual libraries could result in creating hundreds of files in the theme directory.

Using get_template_part() we can organize our theme directory to include a library directory. Now when we want to use content.php in a number of different templates, we can simply call:

get_template_part( 'library/content');

In Child themes

The gettemplatepart function plays quite an essential role in the child themes concept. Let’s use one of the above mentioned examples:

get_template_part( 'content', 'post');

In a child theme context, this will look for the following templates in the following order:

content-post.php in the child theme; content-post.php in the parent theme; content.php in the child theme; content.php in the parent theme;

The order is important and you need to keep it in mind.

With post types and post formats

In the fall back model we can use non hard-coded values with get_template_part(). For example:

get_template_part( 'navigation', get_post_type() );

Where get_post_type() will return the name of the post type that is currently shown, so if we're on a Post, it'll attempt to load content-post.php and fall back to content.php.

If we're on a Page - content-page.php and content.php.

If we're looking at a custom post type, say a Cars, it will look for navigation-cars.php and fall back to navigation.php.

A more common use case is post formats, with the post’s content area extracted into a template part of its own, like this:

get_template_part( 'content', get_post_format() );

Which will attempt to include content-gallery.php for gallery post format, content-quote.php for quote post format, and so on.

Conclusion

Using the get_template_part() function makes front end development cleaner, and it makes our code more reusable.

The main advantage of re-using a template part is that you can modify a single piece of code and have the changes displayed throughout the site without having to modify every page.

Still not finding what you're looking for?

Contact our support team with any additional questions or concerns.

Contact support