get_template_part

Load a template part into a template.

Description

get_template_part( (string) $slug, (null) $name = null ); 

Makes it easy for a theme to reuse sections of code in a easy to overload way for child themes.

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 .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 -special.php then specify "special".

Parameters (2)

0. $slug (string)
The slug name for the generic template.
1. $name — Optional. (null) => null
The name of the specialised template.

Usage

  1. if ( !function_exists( 'get_template_part' ) ) { 
  2. require_once ABSPATH . WPINC . '/general-template.php'; 
  3.  
  4. // The slug name for the generic template. 
  5. $slug = ''; 
  6.  
  7. // The name of the specialised template. 
  8. $name = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_template_part($slug, $name); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/general-template.php  
  1. function get_template_part( $slug, $name = null ) { 
  2. /** 
  3. * Fires before the specified template part file is loaded. 
  4. * The dynamic portion of the hook name, `$slug`, refers to the slug name 
  5. * for the generic template part. 
  6. * @since 3.0.0 
  7. * @param string $slug The slug name for the generic template. 
  8. * @param string|null $name The name of the specialized template. 
  9. */ 
  10. do_action( "get_template_part_{$slug}", $slug, $name ); 
  11.  
  12. $templates = array(); 
  13. $name = (string) $name; 
  14. if ( '' !== $name ) 
  15. $templates[] = "{$slug}-{$name}.php"; 
  16.  
  17. $templates[] = "{$slug}.php"; 
  18.  
  19. locate_template($templates, true, false);