get_template_part( string $slug, string $name = null, array $args = array() )
Loads a template part into a template.
Contents
Description Description
Provides a simple mechanism for child themes to overload reusable sections of code in the theme.
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.
The template is included using require, not require_once, so you may include the same template part multiple times.
For the $name parameter, if the file is called "{slug}-special.php" then specify "special".
Parameters Parameters
- $slug
-
(string) (Required) The slug name for the generic template.
- $name
-
(string) (Optional) The name of the specialised template.
Default value: null
- $args
-
(array) (Optional) Additional arguments passed to the template.
Default value: array()
Return Return
(void|false) Void on success, false if the template does not exist.
More Information More Information
Usage Usage
get_template_part( $slug );
get_template_part( $slug, $name );
Note: get_template_part()
fails silently
Source Source
File: wp-includes/general-template.php
function get_template_part( $slug, $name = null, $args = array() ) { /** * Fires before the specified template part file is loaded. * * The dynamic portion of the hook name, `$slug`, refers to the slug name * for the generic template part. * * @since 3.0.0 * @since 5.5.0 The `$args` parameter was added. * * @param string $slug The slug name for the generic template. * @param string|null $name The name of the specialized template. * @param array $args Additional arguments passed to the template. */ do_action( "get_template_part_{$slug}", $slug, $name, $args ); $templates = array(); $name = (string) $name; if ( '' !== $name ) { $templates[] = "{$slug}-{$name}.php"; } $templates[] = "{$slug}.php"; /** * Fires before a template part is loaded. * * @since 5.2.0 * @since 5.5.0 The `$args` parameter was added. * * @param string $slug The slug name for the generic template. * @param string $name The name of the specialized template. * @param string[] $templates Array of template files to search for, in order. * @param array $args Additional arguments passed to the template. */ do_action( 'get_template_part', $slug, $name, $templates, $args ); if ( ! locate_template( $templates, true, false, $args ) ) { return false; } }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
5.5.0 | The $args parameter was added. |
3.0.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Using with theme subfolders
To use this function with subfolders in your theme directory, simply prepend the folder name before the slug. For example, if you have a folder called “partials” in your theme directory and a template part called “content-page.php” in that sub-folder, you would use
get_template_part()
like this:the old codex had this entry about “Passing Variables to Template”
Because the template is being required, it will not have access to any variables you define within the calling theme’s PHP code, unless you explicitly declare them as global.
However, load_template(), which is called indirectly by get_template_part() extracts all of the WP_Query query variables, into the scope of the loaded template. So you can use set_query_var() to make your variable available to the template part.
Feedback
Minor suggestion, it should rather be ‘Because of where in the code execution the template is being required’. The fact that is is being required vs being included is not the reason one has to use the WP_Query query variables. — By Jonathan Bossenger —
// Since WP version 5.5, we can set the args directly like the following // Note: array method requires PHP 7.* $args = [ ‘my_var_1’ => $my_var_1, ‘my_var_2’ => $my_var_2, ]; get_template_part( ‘content’, ‘part’, $args ); — By Santukon —
Using loop.php in child themes
Assuming the theme folder is
wp-content/themes
, that the parent theme is twentyten, and the child theme is twentytenchild, then the following code —will do a PHP
require()
for the first file that exists among these, in this priority:wp-content/themes/twentytenchild/loop-index.php
wp-content/themes/twentyten/loop-index.php
wp-content/themes/twentytenchild/loop.php
wp-content/themes/twentyten/loop.php
Passing variables after WordPress 5.5 update
======================================
When calling function
In your template part
Navigation
Adding a navigation bar to theme using a generic
nav.php
template file:Get a specific file
Although this kind of defeats the purpose of this function, it’s also possible to load a specific template file with it. All you have to do is use just one argument:
will include
layout.php
from template-parts subdirectory placed in the root of your theme folder.A simple example of how to use the
$args
parameter in WordPress 5.5 in your template parts.In your template part.
Output:
Since WP 5.5 the $args parameter is used to pass variables through the template.
If your web server is using ModSecurity, it will likely block attempts to inline SVGs using a PHP method like
file_get_contents()
. You can useget_template_part()
to inline SVGs.icon.svg
becomesinline-icon.svg.php
get_template_part( 'images/inline', 'icon.svg' );
At the time of writing, this works to get around the default OWASP rule set for ModSecurity. Not tested with other rule sets.
You can also access the template variables in your template part file by doing this:
Your
get_template_part
will look something like this:– Keep in mind that the third argument
$args
has been added since version 5.5Feedback
WordPress PHP coding standards do not permit the use of
extract()
. — By crstauf —How to passing variables to get_template_part() correctly: Wordpress + 5.5
1- Setup variable
2- Passing variable to get_template_part
3- get data in part of template
Passing variables to get_template_part()
It’s not possible to pass variables to a template loaded via get_template_part(). I came up with a workaround that doesn’t rely on global variables for it to work.
Now, when we try to use $is_featured, it will be available to content.php only.
Feedback
Since WP 5.5 this is no longer needed. Use the $args parameter. — By jabbadu —
WordPress PHP coding standards do not permit the use of extract() — By Iakovos Frountas —