get_single_template

函数


get_single_template ( No parameters )
返回值
  • (string) Full path to single template file.
相关
  • get_query_template()
定义位置
  • wp-includes/template.php
    , line 547
引入
1.5.0
弃用

检索当前或父模板中的单个模板的路径。适用于单个文章。单一附件,和单一自定义文章类型。

这个模板的层次结构看起来像。

  1. {Post Type Template}.php
  2. single-{post_type}-{post_name}.php
  3. single-{post_type}.php
  4. single.php

这方面的一个例子是

  1. templates/full-width.php
  2. single-post-hello-world.php
  3. single-post.php
  4. single.php

模板层次和模板路径可通过{@see ‘$type_template_hierarchy’}和{@see ‘$type_template’}动态钩子过滤,其中`$type`为’single’。

function get_single_template() {
	$object = get_queried_object();

	$templates = array();

	if ( ! empty( $object->post_type ) ) {
		$template = get_page_template_slug( $object );
		if ( $template && 0 === validate_file( $template ) ) {
			$templates[] = $template;
		}

		$name_decoded = urldecode( $object->post_name );
		if ( $name_decoded !== $object->post_name ) {
			$templates[] = "single-{$object->post_type}-{$name_decoded}.php";
		}

		$templates[] = "single-{$object->post_type}-{$object->post_name}.php";
		$templates[] = "single-{$object->post_type}.php";
	}

	$templates[] = 'single.php';

	return get_query_template( 'single', $templates );
}