get_template_part()WP 3.0.0

Searches for and connects the specified theme file (first check the file in the child theme, if there is one).

Like PHP include(), but no need to specify the path to the template.

This function is used to include the template part into a template, for example, to add a file responsible for: breadcrumbs, pagination, menu, etc.

// includes breadcrumbs.php from the current template's directory
get_template_part( 'breadcrumbs' );

// includes breadcrumbs-footer.php from the current template's directory
get_template_part( 'breadcrumbs', 'footer' );

If the specified file doesn't exist, nothing will happen.

Child themes

When using a child theme, this function will first try to include a file from the child theme, and if it isn't there, the file from the parent theme will be included.

This functionality allows users to override the parent's theme files in the child theme – create a file with the same name in the child theme. It's a suitable method because the modified files are persistent across updates of the parent theme.

1 time — 0.000044 sec (very fast) | 50000 times — 0.16 sec (very fast) | PHP 7.1.2, WP 4.7.3

Return

null|false. Void on success, false if the template does not exist.

Usage

get_template_part( $slug, $name, $args );
$slug(string) (required)
The slug name for the generic template.
$name(string|null)
The name of the specialized template.
Default: null
$args(array)
Additional arguments passed to the template.
Default: empty array

Examples

0

#1 Include loop.php file in child theme

The name of the parent theme is twentyten; the name of the child theme is twentytenchild. The below code will try to include the following files in the following order:

<?php get_template_part( 'loop', 'index' ); ?>
  1. wp-content/themes/twentytenchild/loop-index.php
  2. wp-content/themes/twentytenchild/loop.php
  3. wp-content/themes/twentyten/loop-index.php
  4. wp-content/themes/twentyten/loop.php
0

#2 Include navigation file (nav.php)

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

#3 Include files from theme sub-directories

get_template_part( 'inc/nav' );
// includes inc/nav.php

If you need to include inc/nav-single.php:

get_template_part( 'inc/nav', 'single' );

// or:

get_template_part( 'inc/nav-single' );

In the first case, the inc/nav-single.php file will be included first, and if it does not exist then inc/nav.php. In the second, only inc/nav-single.php.

0

#4 Parameter Transmission

$params = [ 'param1' => 'hello', 'param2' => [ 1, 2 ] ];

get_template_part( 'inc/nav-single.php', 'single', $params );

Now in the file inc/nav-single.php the specified parameters are in the $args variable. Example code of such a file:

<?php

echo $args['param1']; //> hello

Changelog

Since 3.0.0 Introduced.
Since 5.5.0 A return value was added.
Since 5.5.0 The $args parameter was added.

get_template_part() code WP 6.5.3

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 or null if
	 *                          there is none.
	 * @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 an attempt is made to locate and load a template part.
	 *
	 * @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 or an empty
	 *                            string if there is none.
	 * @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;
	}
}