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

前端 未结 5 2070
慢半拍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:26

    In addition to previous answers, if your menu items are Categories and you want to highlight them when navigating through posts, check also for current-post-ancestor:

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

提交回复
热议问题