我在类别的存档页面中显示父类别的唯一方法是采用以下方法:https://yonkov.github.io/post/add-parent-category-to-the-body-class/
基本上,您需要创建一个类变量,然后将其传递给body类:
<?php
$this_category = get_category($cat);
//If category is parent, list it
if ($this_category->category_parent == 0) {
$this_category->category_parent = $cat;
} else { // If category is not parent, list parent category
$parent_category = get_category($this_category->category_parent);
//get the parent category id
$ParentCatId = $parent_category->cat_ID;
}
$childcategories = array();
$catx = $ParentCatId;
$cats = get_categories(\'parent=\'.$catx);
foreach($cats as $cat) {
$childcategories[] = $cat->term_id; } ;
//if it is a child category, add a class with the parent category id
if( is_category( $childcategories ) ) {
$class = \'parent-category-\'.$ParentCatId;
}
?>