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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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