php - prevent category page from also highlighting blog-page menu

admin2025-05-31  0

I created a "category" menu in Appeareances > menu, which appears in my top menu. Then when I am on this page and this menu is active (class : current-menu-item), my "blog" menu, which is the posts page, is also active with the class current-menu-item.

I'd like to have only my category menu active when I am on this page. Any idea what to tune in the functions.php ?

I am using my own child theme of BeTheme.

Thanks

I created a "category" menu in Appeareances > menu, which appears in my top menu. Then when I am on this page and this menu is active (class : current-menu-item), my "blog" menu, which is the posts page, is also active with the class current-menu-item.

I'd like to have only my category menu active when I am on this page. Any idea what to tune in the functions.php ?

I am using my own child theme of BeTheme.

Thanks

Share Improve this question asked Apr 19 at 12:26 LouisLouis 1532 silver badges10 bronze badges 1
  • 1 I try with other themes and I don't have this problem. this can come from the parent theme, you should ask the developers of this theme. – mmm Commented Apr 19 at 19:02
Add a comment  | 

1 Answer 1

Reset to default 0

Add this to your child theme’s functions.php:

add_filter('nav_menu_css_class', 'fix_category_menu_active_class', 10, 2);

function fix_category_menu_active_class($classes, $item) {
    // If we're on a category archive
    if (is_category()) {
        // Get the ID of the blog page (posts page)
        $blog_page_id = get_option('page_for_posts');

        // If this menu item is the blog page
        if ($item->object_id == $blog_page_id) {
            // Remove the active classes
            $classes = array_diff($classes, array(
                'current-menu-item',
                'current_page_item',
                'current_page_parent',
                'current-menu-ancestor'
            ));
        }
    }
    return $classes;
}

How it works:

  • It checks if you're on a category archive.
  • Then removes the current-menu-item class from the blog menu item if it's being wrongly marked active.
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748637815a313699.html

最新回复(0)