3

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?

2 Answers 2

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, ...).

1
  • FWIW this seems to affect only half the question: marking term active on term archive. I don't see how it would affect posts. Also it seems that in the most recent WP, it's no longer needed for the archive screen, which automatically gets the .current-menu-item class. Fair game, this answer is ancient! Just documenting for the future.
    – jerclarke
    Commented Apr 19 at 16:26
0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.