get_category_template

函数
get_category_template ( No parameters )
返回值
  • (string) Full path to category template file.
相关
  • get_query_template()
定义位置
相关方法
get_author_templateget_date_templateget_query_templateget_tag_templateget_page_template
引入
1.5.0
弃用
-

get_category_template: 这个函数检索应该用于显示给定类别的模板文件。

检索当前或父模板中的类别模板的路径。

这个模板的层次结构看起来像:

1. category-{slug}.php
2. category-{id}.php
3. category.php

这方面的一个例子是:

1. category-news.php
2. category-2.php
3. category.php

模板层次和模板路径可通过{@see ‘$type_template_hierarchy’}和{@see ‘$type_template’}动态钩子过滤,其中`$type`为’类别’。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_category_template() {
$category = get_queried_object();
$templates = array();
if ( ! empty( $category->slug ) ) {
$slug_decoded = urldecode( $category->slug );
if ( $slug_decoded !== $category->slug ) {
$templates[] = "category-{$slug_decoded}.php";
}
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
}
$templates[] = 'category.php';
return get_query_template( 'category', $templates );
}
function get_category_template() { $category = get_queried_object(); $templates = array(); if ( ! empty( $category->slug ) ) { $slug_decoded = urldecode( $category->slug ); if ( $slug_decoded !== $category->slug ) { $templates[] = "category-{$slug_decoded}.php"; } $templates[] = "category-{$category->slug}.php"; $templates[] = "category-{$category->term_id}.php"; } $templates[] = 'category.php'; return get_query_template( 'category', $templates ); }
function get_category_template() {
	$category = get_queried_object();

	$templates = array();

	if ( ! empty( $category->slug ) ) {

		$slug_decoded = urldecode( $category->slug );
		if ( $slug_decoded !== $category->slug ) {
			$templates[] = "category-{$slug_decoded}.php";
		}

		$templates[] = "category-{$category->slug}.php";
		$templates[] = "category-{$category->term_id}.php";
	}
	$templates[] = 'category.php';

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

常见问题

FAQs
查看更多 >