admin管理员组文章数量:1406063
I am not a coder, but I usually get by with Wordpress by doing my research and find my solution. I can't find what I need to do this time so I have attempted to crib together some code - what I am attempting to do is, when I am on the Category Archive, I want to add a body class of the category parent. This is what I have tried and it is working apart from I am getting the parents category ID, but I want the slug/nicename:
add_filter('body_class','hw_custom_body_class');
function hw_custom_body_class($classes){
if(is_category()){
$categories = get_the_category();
$category = strtolower($categories[0]->category_parent);
$classes[]='category-'.$category;
return $classes; }}
I am not a coder, but I usually get by with Wordpress by doing my research and find my solution. I can't find what I need to do this time so I have attempted to crib together some code - what I am attempting to do is, when I am on the Category Archive, I want to add a body class of the category parent. This is what I have tried and it is working apart from I am getting the parents category ID, but I want the slug/nicename:
add_filter('body_class','hw_custom_body_class');
function hw_custom_body_class($classes){
if(is_category()){
$categories = get_the_category();
$category = strtolower($categories[0]->category_parent);
$classes[]='category-'.$category;
return $classes; }}
Share
Improve this question
asked Oct 2, 2013 at 14:51
Andrew SmartAndrew Smart
1311 silver badge2 bronze badges
2 Answers
Reset to default 5Use get_ancestors()
to get the parent terms. Here is an excerpt from my plugin T5 Parent Terms in body_class:
$ancestors = get_ancestors(
get_queried_object_id(),
get_queried_object()->taxonomy
);
if ( empty ( $ancestors ) )
{
return $classes;
}
foreach ( $ancestors as $ancestor )
{
$term = get_term( $ancestor, get_queried_object()->taxonomy );
$new_classes[] = esc_attr( "parent-$term->taxonomy-$term->slug" );
}
This will work with any taxonomy, not just categories.
The only way I got to display the parent category in the category's archive page was by following this approach: https://yonkov.github.io/post/add-parent-category-to-the-body-class/
Basically, you need to create a class variable and then pass it to the body class:
<?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;
}
?>
本文标签: functionsAdd body class of category parent
版权声明:本文标题:functions - Add body class of category parent 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744958750a2634519.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论