admin管理员组文章数量:1332377
I have website with pages which are named the same as categories. For example I have page called Cities
and also I have category called Cities
. I want to add class to navbar item when user is reading post from specific category. So when I'm reading post from category Cities
I want to highlight link in my navbar which going to page Cities
. I made simple function
add_filter('nav_menu_css_class' , 'my_nav_special_class' , 10 , 2);
function my_nav_special_class($classes, $item){
global $post;
$category = get_the_category();
$slug = $category[0]->slug;
$slug = str_replace('-', '', strtolower($slug));
$menu_item = sanitize_title( $item->title );
$menu_item = str_replace('-', '', strtolower($menu_item));
if( is_single() && $slug === $menu_item) {
$classes[] = 'current_page_item';
}
return $classes;
}
When the post category match navbar item I'm adding css class. The problem is when post category is for example Tiger
and in navbar I have page called Animals
. In this case I want to highlight navbar item too. My question is - how can I do this? How the condition should look like when I want to add css class to specific menu item when post have specific cat or cat parent?
I have website with pages which are named the same as categories. For example I have page called Cities
and also I have category called Cities
. I want to add class to navbar item when user is reading post from specific category. So when I'm reading post from category Cities
I want to highlight link in my navbar which going to page Cities
. I made simple function
add_filter('nav_menu_css_class' , 'my_nav_special_class' , 10 , 2);
function my_nav_special_class($classes, $item){
global $post;
$category = get_the_category();
$slug = $category[0]->slug;
$slug = str_replace('-', '', strtolower($slug));
$menu_item = sanitize_title( $item->title );
$menu_item = str_replace('-', '', strtolower($menu_item));
if( is_single() && $slug === $menu_item) {
$classes[] = 'current_page_item';
}
return $classes;
}
When the post category match navbar item I'm adding css class. The problem is when post category is for example Tiger
and in navbar I have page called Animals
. In this case I want to highlight navbar item too. My question is - how can I do this? How the condition should look like when I want to add css class to specific menu item when post have specific cat or cat parent?
1 Answer
Reset to default 0It seems like you should use hierarchies of categories, because it sounds like 'Tiger' belongs to the group 'Animals'.
You can get the parent category with $category->parent
so if page category was Tiger, and parent category was Animals, this would then work.
Let me know if that would work for you and if you need more info
本文标签: categoriesHow to add class to specific navbar item when post parent category is in specific category
版权声明:本文标题:categories - How to add class to specific navbar item when post parent category is in specific category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742305643a2449862.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论