admin管理员组文章数量:1122846
I have some categories on my WP website. When users sees this categories and posts from them i want to mark one of menu items as active.
How can i set one of my menu items to have class "current-menu-item" for this categories and posts?
I have some categories on my WP website. When users sees this categories and posts from them i want to mark one of menu items as active.
How can i set one of my menu items to have class "current-menu-item" for this categories and posts?
Share Improve this question asked Sep 26, 2015 at 10:11 moonvadermoonvader 3325 silver badges17 bronze badges2 Answers
Reset to default 2I use this functions. First of all, you have to add some custom class to your menu item (allow class input in Screen options, it's not visible by default).
function mark_menu_item_as_active($classes, $item) {
if( in_array('my-custom-class',$classes) && ( is_category('my-category') /* OR ...*/ ) ) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter('nav_menu_css_class', 'mark_menu_item_as_active', 10, 2);
This function iterates through all menu items, and you will find your target item with that custom menu class. Then you will check whatever conditions you have (is_category, ...), and add another class to its classes (current-menu-item
, ...).
Marking terms in menus as active while on the term archive screen
This question is very old and it seems that since it was asked, this became part of core, or at least it already works for me, so the solution below has the section that causes this commented out. If for some reason that isn't already working for you, uncomment those lines to turn it on.
Marking terms in menus as active when looking at a post that has the term
The code below (you can paste it in functions.php if you aren't sure what to do with it) will add the .current-menu-item
class to any taxonomy term menu item (tested with category and a custom taxonomy) when you visit a post/page/custom post type with the term.
It's pretty straightforward, the hard part is just making sense of the weird $item
object which is always a WP_Post
, even when the properties correctly point to a taxonomy term.
function gv_filter_nav_menu_css_class_to_make_terms_active( $classes, $item ) {
// Only apply this to taxonomy terms
if ( !isset( $item->type ) OR ( 'taxonomy' !== $item->type )) {
return $classes;
}
// It seems that if we're on the archive page, this already works
// if (is_tax($item->object, $item->object_id)) {
// $classes[] = 'current-menu-item';
// }
// We only want to affect posts or other post types
if (!is_singular()) {
return $classes;
}
// It seems ->object contains the taxonomy type slug and ->object_id has the taxonomy id, weirdly $object is still a WP_Post in this scenario
if (!isset($item->object) OR !isset($item->object_id)) {
return $classes;
}
if (is_object_in_term(get_queried_object_id(), $item->object, $item->object_id)) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter('nav_menu_css_class', 'gv_filter_nav_menu_css_class_to_make_terms_active', 10, 2);
Of course this solution takes for granted you have correct styling of the .current-menu-item
class already set up in your theme, which you probably do!
本文标签: postsMark menu item as currentmenuitem for category
版权声明:本文标题:posts - Mark menu item as current-menu-item for category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309906a1934188.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论