作为对@Rarst sugestion的补充,我需要该函数来使用顶级父模板。
function new_subcategory_hierarchy() {
$category = get_queried_object();
$parent_id = $category->category_parent;
// Create replacement $templates array
$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 {
// Start from the current term
$parent = get_category($parent_id);
所以我在这里加上:
// Climb up the hierarchy until we reach a term with parent = \'0\'
while($parent->parent != \'0\'){
$term_id = $parent->parent;
$parent = get_term_by( \'id\', $term_id, $category->taxonomy);
}
结束时间:
// Current first
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.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\' );