is_page_template

函式
is_page_template ( $template = '' )
引數
  • (string|string[]) $template The specific template filename or array of templates to match.
    Required:
    Default: (empty)
返回值
  • (bool) True on success, false on failure.
定義位置
相關方法
get_page_templateget_paged_templateget_page_templatesget_templateget_page_template_slug
引入
2.5.0
棄用
-

is_page_template: 這個函式檢查當前頁面是否使用了一個特定的頁面模板。它將模板名稱作為引數,如果頁面使用該模板,則返回true,如果不使用則返回false。

確定當前的文章是否使用了一個頁面模板。

這個模板標籤允許你確定你是否在一個頁面模板中。你可以選擇提供一個模板檔名或模板檔名的陣列,然後檢查將特定於該模板。

關於這個和類似主題功能的更多資訊,請檢視《主題開發者手冊》中的{@link Conditional Tags}文章。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function is_page_template( $template = '' ) {
if ( ! is_singular() ) {
return false;
}
$page_template = get_page_template_slug( get_queried_object_id() );
if ( empty( $template ) ) {
return (bool) $page_template;
}
if ( $template == $page_template ) {
return true;
}
if ( is_array( $template ) ) {
if ( ( in_array( 'default', $template, true ) && ! $page_template )
|| in_array( $page_template, $template, true )
) {
return true;
}
}
return ( 'default' === $template && ! $page_template );
}
function is_page_template( $template = '' ) { if ( ! is_singular() ) { return false; } $page_template = get_page_template_slug( get_queried_object_id() ); if ( empty( $template ) ) { return (bool) $page_template; } if ( $template == $page_template ) { return true; } if ( is_array( $template ) ) { if ( ( in_array( 'default', $template, true ) && ! $page_template ) || in_array( $page_template, $template, true ) ) { return true; } } return ( 'default' === $template && ! $page_template ); }
function is_page_template( $template = '' ) {
	if ( ! is_singular() ) {
		return false;
	}

	$page_template = get_page_template_slug( get_queried_object_id() );

	if ( empty( $template ) ) {
		return (bool) $page_template;
	}

	if ( $template == $page_template ) {
		return true;
	}

	if ( is_array( $template ) ) {
		if ( ( in_array( 'default', $template, true ) && ! $page_template )
			|| in_array( $page_template, $template, true )
		) {
			return true;
		}
	}

	return ( 'default' === $template && ! $page_template );
}

常見問題

FAQs
檢視更多 >