您链接到的代码的主要优点是使用category_template
过滤器,用于修改WordPress在用户查看木材类别的子类别时将使用的模板。我为您整理了这些内容,因为当您只使用一个模板时,它会更简单一些:
// add this filter to your themes functions.php file
add_filter(\'category_template\', \'yourprefix_use_lumber_category_template_for_child_categories\');
function yourprefix_use_lumber_category_template_for_child_categories( $template ) {
// get the current category object
$category = get_queried_object();
// if current category has lumber category(assuming lumber category id == 7) as its parent...
if( $category->parent == 7 ) {
// ...then find the named category-lumber.php template in theme, and replace whatever default $template is found by WordPress...
$template = locate_template(\'category-lumber.php\');
// ...finally return the lumber category template file
return $template;
}
else {
// ...the current category has no lumber category as parent, so just return default $template file found by WordPress
return $template;
}
}