【发布时间】:2019-10-10 18:51:54
【问题描述】:
我尝试为指定类别构建自定义模板,我的类别是新闻,我创建了一个名称为 "category-news.php" 的模板,在 "新闻" 类别中我有一些子类别,现在我想应用父类别子类别的类别模板,我在下面测试了这段代码,但它不起作用。
function wp_category_template() {
$category = get_queried_object();
$parent_id = $category->category_parent;
$templates = array();
if ( $parent_id == 0 ) {
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
$templates[] = 'category.php';
} else {
$parent = get_category( $parent_id );
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
$templates[] = "category-{$parent->slug}.php";
$templates[] = "category-{$parent->term_id}.php";
$templates[] = 'category.php';
}
return locate_template( $templates );
}
add_filter( 'category_template', 'wp_category_template' );
【问题讨论】:
标签: wordpress categories