描述:
将一个模板加载到另一个模板中。
用法:
为子主题提供一种简单的机制来加载主题中可重用的代码部分。
包括主题的命名模板部分,或者如果指定了名称,则将包括专用部分。如果主题不包含{slug} .php文件,则不包含模板。
使用require而不是require_once来包含模板,因此您可以多次包含同一模板部分。
对于$ name参数,如果文件名为“ {slug} -special.php”,则指定“ special”。
参数:
$slug
(string) (必填) 通用模板的别名
$name
(string) (选填) 专用模板的名称
默认值: null
源文件:
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 ); }