是的,这可以通过“template\\u redirect”操作实现
我留下来写代码,为自己找到子类别!
function my_subcat_template() {
if ( <is sub category>) {
include (TEMPLATEPATH . \'/category-fun.php\');
exit;
}
}
add_action(\'template_redirect\', \'my_subcat_template\');
更新这是一个更好的问题答案。这是函数。php
它所做的是重新定义wordpress处理类别(和子类别)模板的方式
function new_subcategory_hierarchy() {
$category = get_queried_object();
$parent_id = $category->category_parent;
$templates = array();
if ( $parent_id == 0 ) {
// Use default values from get_category_template()
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
$templates[] = \'category.php\';
} else {
// Create replacement $templates array
$parent = get_category( $parent_id );
// Current first
$templates[] = "subcategory-{$category->slug}.php";
$templates[] = "subcategory-{$category->term_id}.php";
$templates[] = "subcategory.php";
// Parent second
$templates[] = "category-{$parent->slug}.php";
$templates[] = "category-{$parent->term_id}.php";
$templates[] = \'category.php\';
}
return locate_template( $templates );
}
add_filter( \'category_template\', \'new_subcategory_hierarchy\' );
现在您可以使用
subcategory-slug.php
或
subcategory-id.php
对于特定子类别
或subcategory.php
一般而言,适用于所有子类别