get_template_part()
get_template_part( string $slug, string $name = null ) …
get_template_part( string $slug, string $name = null )
将模板零件加载到模板中。
Loads a template part into a template.
目录锚点:#说明#参数#返回#源码#笔记
说明(Description)
为子主题提供一种简单的机制,用于重载主题中可重用的代码段。
包括主题的命名模板部分,或者如果指定了名称,则将包括专门的部分。如果主题不包含{slug}.php文件,则不包含模板。
模板使用require包含,而不是require_once,因此可以多次包含同一模板部件。
对于$name参数,如果文件名为“{slug}”-特殊.php然后指定“特殊”。
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| $slug | (string) | 必需 | 通用模板的slug名称。 |
| $name | (string) | 可选 | 专业模板的名称。 |
返回(Return)
无返回值
源码(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);
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 3.0.0 | wp-includes/general-template.php:135 | 0 | 4 |
笔记(Notes)
与主题子文件夹一起使用
使用循环.php在儿童主题中
旧的codex有一个关于“将变量传递给模板”的条目
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。





还没有任何评论,赶紧来占个楼吧!