返回介绍

get_page_template()

发布于 2017-09-10 23:43:57 字数 3322 浏览 1084 评论 0 收藏 0

get_page_template()

Retrieve path of page template in current or parent template.


description

The hierarchy for this template looks like:

  1. {Page Template}.php
  2. page-{page_name}.php
  3. page-{id}.php
  4. page.php

An example of this is:

  1. page-templates/full-width.php
  2. page-about.php
  3. page-4.php
  4. page.php

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


返回值

(string) Full path to page template file.


源代码

File: wp-includes/template.php

function get_page_template() {
	$id = get_queried_object_id();
	$template = get_page_template_slug();
	$pagename = get_query_var('pagename');

	if ( ! $pagename && $id ) {
		// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
		$post = get_queried_object();
		if ( $post )
			$pagename = $post->post_name;
	}

	$templates = array();
	if ( $template && 0 === validate_file( $template ) )
		$templates[] = $template;
	if ( $pagename ) {
		$pagename_decoded = urldecode( $pagename );
		if ( $pagename_decoded !== $pagename ) {
			$templates[] = "page-{$pagename_decoded}.php";
		}
		$templates[] = "page-{$pagename}.php";
	}
	if ( $id )
		$templates[] = "page-{$id}.php";
	$templates[] = 'page.php';

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

更新日志

Versiondescription
4.7.0The decoded form of page-{page_name}.php was added to the top of the template hierarchy when the page name contains multibyte characters.
1.5.0Introduced.

相关函数

Uses

  • wp-includes/query.php: get_queried_object_id()
  • wp-includes/query.php: get_query_var()
  • 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()
  • Show 1 more use Hide more uses

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    Display the filename of the page template used to render a page (printed within an HTML comment, in this example) :

    
    <?php echo '<!-- ' . basename( get_page_template() ) . ' -->'; ?>
    
    
  2. The link for $type_template is not found on this page. It needs updating.

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

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

发布评论

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