我设法以程序方式解决了这个问题:
1 在主题功能中添加了这个片段:
add_action('template_redirect', 'load_category_tree_template');
function load_category_tree_template() {
if (is_category() && !is_feed()) {
// replace 'your-base-category-slug' with the slug of root category
$base_tree_cat_id = get_cat_id('your-base-category-slug');
// get current category id
$catid = get_query_var('cat');
if (is_category($base_tree_cat_id) || cat_is_ancestor_of($base_tree_cat_id, $catid)) {
load_template(STYLESHEETPATH . '/category-your-base-category-slug.php');
exit;
}
}
}
然后在 category-your-root-category-slug.php 文件中,我根据当前类别 slug 包含一个不同的其他文件:
<?php get_header(); ?>
<?php
if (is_category( )) {
$cat = get_query_var('cat');
$currentcat = get_category ($cat);
$currentslug = $currentcat->slug;
}
// if we are in base category we know it by the slug
if ($currentslug == 'your-base-category-slug'){
// if we are load a file with the loop for the root category
require_once('post-loop-caregory-tree-root.php');
}else{
// otherwise we are not in the root, so show the loop for all other category in the tree
require_once('post-loop-category-tree.php');
}
?>
<?php get_footer(); ?>
我知道它可以做得更好,但最终它是非常简单的解决方案。