admin管理员组文章数量:1390192
There's a few posts about getting sub-categories to use the same template as their parent but I want to do something slightly different.
I have a category 67, and I want all sub-categories of 67 to use a specific template. Not the default template, and not the custom category-67.php template.
How do I do that?
I have the following code in functions.php, but it also seems to change the template of category-67
// use specific template depending on category
function myTemplateSelect() {
if (is_category() && !is_feed()) {
if (is_category(get_cat_id('67')) || cat_is_ancestor_of(get_cat_id('67'), get_query_var('cat'))) {
load_template(TEMPLATEPATH . '/category-slider.php');
exit;
}
}
}
add_action('template_redirect', 'myTemplateSelect');
Eventually I will want to add a few more categories to this also.
Any ideas?
Thanks
There's a few posts about getting sub-categories to use the same template as their parent but I want to do something slightly different.
I have a category 67, and I want all sub-categories of 67 to use a specific template. Not the default template, and not the custom category-67.php template.
How do I do that?
I have the following code in functions.php, but it also seems to change the template of category-67
// use specific template depending on category
function myTemplateSelect() {
if (is_category() && !is_feed()) {
if (is_category(get_cat_id('67')) || cat_is_ancestor_of(get_cat_id('67'), get_query_var('cat'))) {
load_template(TEMPLATEPATH . '/category-slider.php');
exit;
}
}
}
add_action('template_redirect', 'myTemplateSelect');
Eventually I will want to add a few more categories to this also.
Any ideas?
Thanks
Share Improve this question asked Feb 27, 2015 at 11:39 MikedefieslifeMikedefieslife 1611 silver badge7 bronze badges1 Answer
Reset to default 6I would recommend using the category_template
filter - just check if the current category is an ancestor of 67
:
function wpse_179617_category_template( $template ) {
if ( cat_is_ancestor_of( 67, get_queried_object_id() /* The current category ID */ ) )
$template = locate_template( 'category-slider.php' );
return $template;
}
add_filter( 'category_template', 'wpse_179617_category_template' );
本文标签: How do I set a specific template for subcategories
版权声明:本文标题:How do I set a specific template for sub-categories? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744612825a2615757.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论