$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - prevent category page from also highlighting blog-page menu|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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.
Post a comment

comment list (0)

  1. No comments so far