WordPress at a glance

get_template_part() WP 3.0.0

Searches for and connects the specified theme file. Like 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 – just create a file with the same name in the child theme. It's a good method because so the modified files are persistent across updates of the parent theme.

Works based on: locate_template()
✈ 1 time = 0.000044s = very fast | 50000 times = 0.16s = very fast | PHP 7.1.2, WP 4.7.3
Return

Null. Nothing.

Usage

get_template_part( $slug, $name );
$slug(string) (required)
The slug name for the generic template.
$name(string)
The name of the specialised template.
Default: null

Examples

#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

#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) ?>

#3 Include files from theme subdirectories

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.

Changelog

Since 3.0.0 Introduced.

Code of get template part: wp-includes/general-template.php WP 5.2.4

<?php
function get_template_part( $slug, $name = null ) {
	/**
	 * 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
	 *
	 * @param string      $slug The slug name for the generic template.
	 * @param string|null $name The name of the specialized template.
	 */
	do_action( "get_template_part_{$slug}", $slug, $name );

	$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
	 *
	 * @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.
	 */
	do_action( 'get_template_part', $slug, $name, $templates );

	locate_template( $templates, true, false );
}

Related Functions

From category: Theme files connection

More from Template Tags: Main Functions

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register