What is the get_template_part function and how to use it in WordPress
Knowing how to customize your WordPress theme is a crucial step in shaping your site to match your vision. Key to helping you achieve this is the get_template_part() function, which allows you to embed parts of your template in various locations without duplicating code.
This article will cover everything you need to know about get_template_part() ‒ what it is, how it works, and how to use it effectively. We’ll also highlight common mistakes to avoid along the way.
What is get_template_part()
The get_template_part() function loads reusable sections of your theme template.
Using this WordPress function helps keep your theme’s code clean and organized. Updates also become simpler ‒ make changes in one file, and they automatically update everywhere the template part is used.
In a team environment, this allows developers to work on different parts of a theme at the same time without overwriting each others’ work. Plus, this function works with child themes, allowing for customization without modifying the parent theme’s files.

Understanding the relationship between themes and template parts
In WordPress, themes control your site’s design and overall structure, while template parts are smaller PHP files containing reusable sections of code.
A theme will include various templates that display content, but most pages share common elements like headers and footers. These elements are called template parts.
Instead of duplicating code across multiple files, you can move template parts into a separate file and use get_template_part() to call them whenever needed.
How to use the get_template_part() function
Here are various ways of using the WordPress template tag, depending on your theme’s structure.
Using the get_template_part() function to call template parts
The most basic way to use get_template_part() is to include a file name, like this:
get_template_part( 'file-name' );
This method works best for incorporating template parts like headers, footers, and sidebars that are used often and don’t need customization for specific conditions.
For example, if you include the following syntax in a page, it will include header.php from your theme’s root directory.
get_template_part( 'header' );
Calling template parts within a directory using the get_template_part() function
If the template part is stored in one of your theme’s subdirectories, add a parameter to the function as follows:
get_template_part( 'folder-name/file-name' );
For example, the syntax below tells WordPress to look for a file named content.php inside the template-parts directory of your theme.
get_template_part( 'template-parts/content' );
Calling fallback files using the get_template_part() function
This method is useful when you want to call a preferred version of a template part, but you also want a backup version in case the first one doesn’t exist.
Simply add parameters to the core function like this:
get_template_part( 'default-file', 'target-file' );
The first parameter (‘default-file‘) specifies the base name of the template part to load, while the second parameter (‘target-file‘) serves as the suffix. WordPress will combine these two parameters and look for a file named default-file-target-file.php.
If that file is missing, WordPress will fall back to the base name file instead of returning nothing.
Here’s an example:
get_template_part( 'template-parts/content', 'post' );
WordPress first looks for content-post.php in the template-parts folder. If it doesn’t exist, WordPress automatically falls back and loads template-parts/content.php from the same folder.
Common mistakes in using the get_template_part() function
The get_template_part() function is a powerful tool in WordPress, but it can present challenges. Here are some common issues users often face when working with this function:
Incorrect file path
Misplacing template parts or failing to specify the correct file path can cause WordPress to think the file doesn’t exist. This usually happens when fetching template parts in folders.
Check whether your code points to the right directory.
Incorrect file naming conventions
If you don’t name your files correctly, WordPress won’t be able to find them. Make sure your file names match exactly, especially when using additional parameters like ‘post’ or ‘page.’
Not using fallback files when needed
Your site can break if a template part is missing, so it’s crucial to have a backup file in place. For example, using content.php as a fallback ensures WordPress has a default option if it can’t locate content-post.php.
Fetching non-reusable code
If you’re only using something once, don’t overcomplicate it by creating a template part. Only use the function for major sections like headers and footers.
Not considering child themes
When using get_template_part() in the parent theme, WordPress will first look for the template part in the child theme.
If you want to override a template part, do so in the child theme’s PHP file and leave the default version in the parent theme as is. This way, you can easily roll back if you change your mind or things don’t work out.
Check out our article on creating a WordPress child theme to learn how child themes work, and to find out how to set one up properly.
Conclusion
Using the get_template_part() function makes WordPress theme development easier. It’s also helpful for keeping your code lean and organized, which could benefit site performance.
It’s easy to use and quite flexible, so give it a try in your next project and see how it enhances your workflow.
We hope this article helped you understand how to make the most out of get_template_part(). Got any questions? Drop us a comment below.
get_template_part() function FAQ
What are the benefits of using get_template_part?
The get_template_part() function keeps your theme organized by avoiding duplicated code. Changes made in one template part will be up to date wherever it’s used. Additionally, it makes it easier for developers to work on different sections at the same time, and lets you customize child themes without changing the parent theme.
How do I load multiple template parts using this function?
The simplest way to load multiple template parts with get_template_part() is to use the function multiple times in your template file, each time for a different section. WordPress will then load the appropriate part for each call.
Is there a performance impact when using get_template_part?
Using get_template_part() doesn’t impact performance much since it helps organize your theme into smaller parts. However, it’s best to avoid overusing it – or misusing it on template parts that will only be used once – and keep your template parts simple. And don’t forget to enable caching to optimize loading speed.