1. 首页
  2. WordPress钩子手册

get_template_part

do_action( ‘get_template_part’, string $slug, string $name, string[] $templates )

动作钩子::在加载模板部件之前触发。
Action Hook: Fires before a template part is loaded.

目录锚点:#参数#源码


参数(Parameters)

参数 类型 说明
$slug (string) 通用模板的slug名称。
$name (string) 专用模板的名称。
$templates (string[]) 按顺序搜索的模板文件数组。

源码(Source)

/**
 * Load a template part into a template
 *
 * 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 {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".
 *
 * @since 3.0.0
 *
 * @param string $slug The slug name for the generic template.
 * @param string $name The name of the specialised template.
 */
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 $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);
}
更新版本 源码位置 使用 被使用
5.2.0 wp-includes/general-template.php:166 1 0

get_template_part 为WP2原创文章,链接:https://www.wp2.cn/hook/get_template_part-2/