Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get template part


Load a template part (other than header, sidebar, footer) into a template. Makes it easy for a theme to reuse sections of code and an easy way for child themes to replace sections of their parent theme.

The template part is located by looking for a file named {slug}.php in the theme (or parent theme, if your theme is a child theme). If the optional $name argument is provided then this function will first look for {slug}-{name}.php, and if it's not found then {slug}.php will be used as a fallback.

If no matching template can be located this function will fail silently, producing neither output nor error messages.


 <?php get_template_part$slug ); ?>   <?php get_template_part$slug$name ); ?> 

get_template_part doesn't return a value and doesn't warn if it fails to find a matching template file.

If you want to hear about failures, use:

   <?php assert( "locate_template( array('$slug-$name.php', '$slug.php'), false, false )" ); ?>


(string) (required) The slug name for the generic template.
Default: None
(string) (optional) The name of the specialized template.
Default: None


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 --

<?php get_template_part( 'loop', 'index' ); ?>

will do a PHP require() for the first file that exists among these, in this priority:

  1. wp-content/themes/twentytenchild/loop-index.php
  2. wp-content/themes/twentyten/loop-index.php
  3. wp-content/themes/twentytenchild/loop.php
  4. wp-content/themes/twentyten/loop.php

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:

<?php get_template_part( 'partials/content', 'page' ); ?>


Adding a navigation bar to theme using a generic nav.php template file:

<?php get_template_part( 'nav' );           // Navigation bar (nav.php) ?>
<?php get_template_part( 'nav', '2' );      // Navigation bar #2 (nav-2.php) ?>
<?php get_template_part( 'nav', 'single' ); // Navigation bar to use in single pages (nav-single.php) ?>


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.

// You wish to make $my_var available to the template part at `content-part.php`
set_query_var( 'my_var', $my_var );
get_template_part( 'content', 'part' );

Change Log

Source File

get_template_part() is located in wp-includes/general-template.php.

External Resources


Include Tags

See also index of Function Reference and index of Template Tags.