The get_template_part() WordPress PHP function loads a template part into a template, providing a simple mechanism for child themes to overload reusable sections of code in the theme.
Usage
get_template_part($slug, $name, $args);
Example:
get_template_part('content', 'archive', array('post_type' => 'post'));
Parameters
$slug
(string) Required: The slug name for the generic template.$name
(string) Optional: The name of the specialized template. Default: null.$args
(array) Optional: Additional arguments passed to the template. Default: array().
More information
See WordPress Developer Resources: get_template_part
Examples
Loading a generic template part
Load the content.php
template part in the theme.
get_template_part('content');
Loading a specialized template part
Load the content-page.php
template part in the theme.
get_template_part('content', 'page');
Using with theme subfolders
Load a template part called content-page.php
in a sub-folder called “partials” in the theme directory.
get_template_part('partials/content', 'page');
Passing variables after WordPress 5.5 update
Pass additional arguments to the template part.
get_template_part('template-part', 'name', array('key' => 'value', 'key2' => 'value2'));
In your template part:
echo $args['key']; // Outputs 'value'
Using set_query_var() to make variables available to the template part
Make $my_var
available to the content-part.php
template part.
set_query_var('my_var', $my_var); get_template_part('content', 'part');