get_template_hierarchy

函式
get_template_hierarchy ( $slug, $is_custom = false, $template_prefix = '' )
引數
  • (string) $slug The template slug to be created.
    Required:
  • (bool) $is_custom Optional. Indicates if a template is custom or part of the template hierarchy. Default false.
    Required:
    Default: false
  • (string) $template_prefix Optional. The template prefix for the created template. Used to extract the main template type, e.g. in `taxonomy-books` the `taxonomy` is extracted. Default empty string.
    Required:
    Default: (empty)
返回值
  • (string[]) The template hierarchy.
定義位置
相關方法
get_page_hierarchy_get_term_hierarchyget_template_partget_template_directoryget_template
引入
6.1.0
棄用
-

get_template_hierarchy: 這個函式檢索WordPress用來顯示一個頁面的模板檔案的列表。它需要一個引數:作為起點使用的模板的名稱。它返回一個模板檔名的陣列。

獲取要建立的給定模板slug的模板層次。

注意:總是新增`index`作為最後的後備模板。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) {
if ( 'index' === $slug ) {
return array( 'index' );
}
if ( $is_custom ) {
return array( 'page', 'singular', 'index' );
}
if ( 'front-page' === $slug ) {
return array( 'front-page', 'home', 'index' );
}
$template_hierarchy = array( $slug );
// Most default templates don't have `$template_prefix` assigned.
if ( $template_prefix ) {
list( $type ) = explode( '-', $template_prefix );
// These checks are needed because the `$slug` above is always added.
if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) {
$template_hierarchy[] = $template_prefix;
}
if ( $slug !== $type ) {
$template_hierarchy[] = $type;
}
}
// Handle `archive` template.
if (
str_starts_with( $slug, 'author' ) ||
str_starts_with( $slug, 'taxonomy' ) ||
str_starts_with( $slug, 'category' ) ||
str_starts_with( $slug, 'tag' ) ||
'date' === $slug
) {
$template_hierarchy[] = 'archive';
}
// Handle `single` template.
if ( 'attachment' === $slug ) {
$template_hierarchy[] = 'single';
}
// Handle `singular` template.
if (
str_starts_with( $slug, 'single' ) ||
str_starts_with( $slug, 'page' ) ||
'attachment' === $slug
) {
$template_hierarchy[] = 'singular';
}
$template_hierarchy[] = 'index';
return $template_hierarchy;
};
function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) { if ( 'index' === $slug ) { return array( 'index' ); } if ( $is_custom ) { return array( 'page', 'singular', 'index' ); } if ( 'front-page' === $slug ) { return array( 'front-page', 'home', 'index' ); } $template_hierarchy = array( $slug ); // Most default templates don't have `$template_prefix` assigned. if ( $template_prefix ) { list( $type ) = explode( '-', $template_prefix ); // These checks are needed because the `$slug` above is always added. if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) { $template_hierarchy[] = $template_prefix; } if ( $slug !== $type ) { $template_hierarchy[] = $type; } } // Handle `archive` template. if ( str_starts_with( $slug, 'author' ) || str_starts_with( $slug, 'taxonomy' ) || str_starts_with( $slug, 'category' ) || str_starts_with( $slug, 'tag' ) || 'date' === $slug ) { $template_hierarchy[] = 'archive'; } // Handle `single` template. if ( 'attachment' === $slug ) { $template_hierarchy[] = 'single'; } // Handle `singular` template. if ( str_starts_with( $slug, 'single' ) || str_starts_with( $slug, 'page' ) || 'attachment' === $slug ) { $template_hierarchy[] = 'singular'; } $template_hierarchy[] = 'index'; return $template_hierarchy; };
function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) {
	if ( 'index' === $slug ) {
		return array( 'index' );
	}
	if ( $is_custom ) {
		return array( 'page', 'singular', 'index' );
	}
	if ( 'front-page' === $slug ) {
		return array( 'front-page', 'home', 'index' );
	}

	$template_hierarchy = array( $slug );

	// Most default templates don't have `$template_prefix` assigned.
	if ( $template_prefix ) {
		list( $type ) = explode( '-', $template_prefix );
		// These checks are needed because the `$slug` above is always added.
		if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) {
			$template_hierarchy[] = $template_prefix;
		}
		if ( $slug !== $type ) {
			$template_hierarchy[] = $type;
		}
	}

	// Handle `archive` template.
	if (
		str_starts_with( $slug, 'author' ) ||
		str_starts_with( $slug, 'taxonomy' ) ||
		str_starts_with( $slug, 'category' ) ||
		str_starts_with( $slug, 'tag' ) ||
		'date' === $slug
	) {
		$template_hierarchy[] = 'archive';
	}
	// Handle `single` template.
	if ( 'attachment' === $slug ) {
		$template_hierarchy[] = 'single';
	}

	// Handle `singular` template.
	if (
		str_starts_with( $slug, 'single' ) ||
		str_starts_with( $slug, 'page' ) ||
		'attachment' === $slug
	) {
		$template_hierarchy[] = 'singular';
	}

	$template_hierarchy[] = 'index';

	return $template_hierarchy;
};

常見問題

FAQs
檢視更多 >