get_template_part
函数
get_template_part ( $slug, $name = null, $args = array() )
- 参数
-
-
(string)
$slug
The slug name for the generic template.- Required: 是
-
(string)
$name
The name of the specialised template.- Required: 否
- Default: null
-
(array)
$args
Optional. Additional arguments passed to the template. Default empty array.- Required: 否
- Default: array()
-
(string)
- 返回值
-
- (void|false) Void on success, false if the template does not exist.
- 定义位置
-
-
wp-includes/general-template.php
, line 167
-
wp-includes/general-template.php
- 引入
- 3.0.0
- 弃用
- –
将一个模板部分加载到一个模板中。
为子主题提供了一个简单的机制,以便在主题中重载可重用的代码部分。
包括一个主题的命名的模板部分,或者如果指定了一个名称,那么一个特殊的部分将被包括。如果主题不包含{slug}.php文件,那么将不包含模板。
模板是使用require而不是require_once来包含的,所以你可以多次包含同一个模板部分。
对于$name参数,如果文件被称为”{slug}-special.php”,那么指定 “special”。
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.
* @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.
* @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;
}
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。


