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

原型

get_page_template()

描述

此模板的层次结构如下所示:

参考:

  • get_query_template()

返回值

(string)  页面模板文件的完整路径。

源文件

路径:wp-includes/template.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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 );
}

...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_page_template/