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 badges
Add a comment  | 

2 Answers 2

Reset to default 2

I 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