^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_page_template()用法示例

get_page_template()

Retrieve path of page template in current or parent template.


描述

The hierarchy for this template looks like:

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

  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


  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.

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈