How to add “active” class to wp_nav_menu() current menu item (simple way)

前端 未结 5 2080
慢半拍i
慢半拍i 2020-12-04 09:17

I am creating custom Wordpress theme using a starter theme _Underscores. I am also using Bootstrap as a front-end framework.

I would like to modify wp_nav_menu so th

5条回答
  •  醉话见心
    2020-12-04 09:40

    To also highlight the menu item when one of the child pages is active, also check for the other class (current-page-ancestor) like below:

    add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
    
    function special_nav_class ($classes, $item) {
        if (in_array('current-page-ancestor', $classes) || in_array('current-menu-item', $classes) ){
            $classes[] = 'active ';
        }
        return $classes;
    }
    

提交回复
热议问题