get_page_template()获取页面模板文件page.php

目录

描述

译文

检索当前模板或父模板的页面模板路径。

函数首先尝试在'_wp_page_template'页面meta数据中寻找文件。若文件存在且不为空,函数继续寻找'page.php'。

原文

Retrieve path of page template in current or parent template.

First attempt is to look for the file in the '_wp_page_template' page meta data. The second attempt, if the first has a file and is not empty, is to look for 'page.php'.

get_page_template() 描述

用法

<?php get_page_template(); ?>

get_page_template() 用法

参数

None.

get_page_template() 参数

返回值

(string) 

Returns path of page template in current or parent template.

get_page_template() 返回值

示例

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php echo '<!-- ' . basename( get_page_template() ) . ' -->'; ?>

get_page_template() 示例

注意

get_page_template() 注意

历史

添加于 版本: 1.5.0

get_page_template() 历史

源文件

get_page_template() 函数的代码位于 wp-includes/template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve path of page template in current or parent template.
 *
 * Will first look for the specifically assigned page template.
 * Then will search for 'page-{slug}.php', followed by 'page-{id}.php',
 * and finally 'page.php'.
 *
 * The template path is filterable via the dynamic {@see '$type_template'} hook,
 * e.g. 'page_template'.
 *
 * @since 1.5.0
 *
 * @see get_query_template()
 *
 * @return string Full path to page template file.
 */
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 )
		$templates[] = "page-$pagename.php";
	if ( $id )
		$templates[] = "page-$id.php";
	$templates[] = 'page.php';

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

get_page_template() 源文件