返回介绍

get_single_template()

发布于 2017-09-11 00:10:22 字数 2576 浏览 1151 评论 0 收藏 0

get_single_template()

Retrieve path of single template in current or parent template. Applies to single Posts, single Attachments, and single custom post types.


description

The hierarchy for this template looks like:

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

An example of this is:

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

The template hierarchy and template path are filterable via the ‘$type_template_hierarchy’ and ‘$type_template’ dynamic hooks, where $type is ‘single’.


返回值

(string) Full path to single template file.


源代码

File: wp-includes/template.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 );
}

更新日志

Versiondescription
4.7.0Post Type Template}.php was added to the top of the template hierarchy
4.4.0single-{post_type}-{post_name}.php was added to the top of the template hierarchy.
1.5.0Introduced.

相关函数

Uses

  • wp-includes/query.php: get_queried_object()
  • wp-includes/functions.php: validate_file()
  • wp-includes/template.php: get_query_template()
  • wp-includes/post-template.php: get_page_template_slug()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文