get_page_template() - WordPress 函数参考中文文档

返回介绍

get_page_template()

发布于 2017-09-10 字数 3322 浏览 998 评论 0

get_page_template()

Retrieve path of page template in current or parent template.


描述

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 );
}

更新日志

Version 描述
4.7.0 The 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.0 Introduced.

相关函数

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.

上一篇:get_page_statuses()

下一篇:get_page_templates()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!