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()
返回值
  • (void|false) Void on success, false if the template does not exist.
定义位置
相关方法
get_templateblock_template_partget_template_hierarchyget_template_directory_get_block_templates_paths
引入
3.0.0
弃用
-

get_template_part: 这个函数检索一个模板部分文件的内容。它需要两个参数:模板部分的文件名,和一个可选的当前主题目录下的子目录名称。它以字符串形式返回模板部分文件的内容。

将一个模板部分加载到一个模板中。

为子主题提供了一个简单的机制,以便在主题中重载可重用的代码部分。

包括一个主题的命名的模板部分,或者如果指定了一个名称,那么一个特殊的部分将被包括。如果主题不包含{slug}.php文件,那么将不包含模板。

模板是使用require而不是require_once来包含的,所以你可以多次包含同一个模板部分。

对于$name参数,如果文件被称为”{slug}-special.php”,那么指定”special”。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
}
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; } }
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;
	}
}

常见问题

FAQs
查看更多 >