-
Notifications
You must be signed in to change notification settings - Fork 0
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).
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.
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.
Get started
Configuration
Build child themes
- Customizing CSS in a child theme
- Overriding templates in a child theme
- Code patterns
- Code reviews
- Pulling in Foundation Updates
- Merging and Creating a Pull Request
Sass
Javascript
PHP
- Coding Standards
- PHP Constants
- Temp PHP Code Patterns
- PHP Snippets
- How to Use Hooks
- Action Hooks
- Using Action Hooks To Output Markup
- Filter Hooks
Shortcodes
Templates
GitHub
Tasks
Contribute to the framework
- Framework Development and Release Workflows
- Documentation Template
- Testing your changes
- Creating a new release
- Migration Guide
- Needs Documentation
Code Examples
- Adding Content Container Classes
- Adding News Templates
- Adding Script Dependencies
- Changing Available Layouts and Default Layout
- Displaying a Fancy Gallery
- Loading a Custom Build of Modernizr
- Loading Modernizr in the Footer
- Using Action Hooks To Output Markup
- Understanding get_template_part
BU Developer Resources