get_page_template

函式
get_page_template ( No parameters )
返回值
  • (string) Full path to page template file.
相關
  • get_query_template()
定義位置
相關方法
get_paged_templateget_page_templatesget_tag_templateget_date_templateget_page_template_slug
引入
1.5.0
棄用
-

get_page_template函式是一個WordPress函式,用於檢索一個頁面的模板名稱: 該函式接受一個引數,即你想檢索模板名稱的頁面的ID: 該函式返回該頁面的模板名稱。

在當前模板或父模板中檢索頁面模板的路徑。

注意:對於區塊主題,使用locate_block_template函式代替。

這個模板的層次結構看起來像:
1. {Page Template}.php
2. page-{page_name}.php
3. page-{id}.php
4. page.php

這方面的一個例子是:

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

模板層次和模板路徑可通過{@see ‘$type_template_hierarchy’}和{@see ‘$type_template’}動態鉤子過濾,其中`$type’為’page’。
和{@see ‘$type_template’}動態鉤子,其中`$type`是’page’。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 );
}
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 ); }
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 );
}

常見問題

FAQs
檢視更多 >