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

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

    In header.php insert this code to show menu:

     'menu-one',
                'walker' => new Custom_Walker_Nav_Menu_Top
            )
        );
    ?>
    

    In functions.php use this:

    class Custom_Walker_Nav_Menu_top extends Walker_Nav_Menu
    {
        function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
            $is_current_item = '';
            if(array_search('current-menu-item', $item->classes) != 0)
            {
                $is_current_item = ' class="active"';
            }
            echo ''.$item->title;
        }
    
        function end_el( &$output, $item, $depth = 0, $args = array() ) {
            echo '
  • '; } }

提交回复
热议问题