Make a nav item active

Have a blog with categories.
Want to make “Blog” active in the nav bar even in a category page.

I don’t want to have any submenu under the Blog.
Appreciate if you would have any idea.

If I understand your scenario correctly you could use WordPress’ nav_menu_css_class hook:

add_filter( 'nav_menu_css_class', function( $classes, $menu_item ) {
    // Change 153 to your blog page ID
    if ( $menu_item->object_id == 153 && is_category() ) {
        $classes[] = 'current-menu-item';
    }
    
    return $classes;
}, 10, 2 );

1 Like

Thank you for your quick feedback. I understood and tried but didn’t work.
The blog has own page, ID = 567 and the Recipes page ID = 1857.

Here’s what I did:
Changed the object_id == 1857 and added the shorcode generated by Code Snippets to the Recipes page but didn’t work.
Did I miss any?
Thanks.

You do not embed a shortcode or something. If you’re using Code Snippets plugin just add my code as a PHP snippet. You could also just add the code to your Bricks child theme’s functions.php.

As I wrote you have to use your blog page ID (in your case 567) in the snippet.

1 Like

Thanks for your help.
Somehow it didn’t work. Added you code to the child theme’s functions.php.

Here’s the page url, Recipes – Hugh Carpenter

Appreciate if you can find any issue.

Your recipe page is not a category (archive) page – as stated in your original question – but a regular page. So in your case it has to be something like this:

add_filter( 'nav_menu_css_class', function( $classes, $menu_item ) {
    $blog_page_id = 567;
    $recipe_page_id = 1857;
    if ( $menu_item->object_id == $blog_page_id && is_page( $recipe_page_id ) ) {
        $classes[] = 'current-menu-item';
    }
    
    return $classes;
}, 10, 2 );

Perfect!
It’s a very useful snippet so that can be used for other pages as well.
Thank you for your patience and dedicated help.