WordPress at a glance

get_template_part() WP 1.0

Loads a template part into a template.

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

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

No Hooks.

Return

Nothing (null).

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

Code of get_template_part: wp-includes/general-template.php VER 5.0

<?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";

	locate_template($templates, true, false);
}

Related Functions

From category: Theme files connection

More from Template Tags: Main Functions

No comments
    Hello, !     Log In . Register