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

原型

get_single_template()

描述

此模板的层次结构如下所示:

参考:

  • get_query_template()

返回值

(string)  单个模板文件的完整路径。

源文件

路径:wp-includes/template.php

<?php
...
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 );
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_single_template/