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

前端 未结 5 2066
慢半拍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;
    }
    
    0 讨论(0)
  • 2020-12-04 09:28

    Just paste this code into functions.php file:

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

    More on wordpress.org:

    • Highlight Current Page in WordPress 3.0 Menus
    • Adding .active class to active menu item
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-04 09:42

    If you want the 'active' in the html:

    header with html and php:

            <?php
            $menu_items = wp_get_nav_menu_items( 'main_nav' ); // id or name of menu
            foreach ( (array) $menu_items as $key => $menu_item ) {
                if ( ! $menu_item->menu_item_parent ) {
                    echo "<li class=" . vince_check_active_menu($menu_item) . "><a href='$menu_item->url'>";
                    echo $menu_item->title;
                    echo "</a></li>";
                }
            }
            ?>
    

    functions.php:

    function vince_check_active_menu( $menu_item ) {
        $actual_link = ( isset( $_SERVER['HTTPS'] ) ? "https" : "http" ) . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        if ( $actual_link == $menu_item->url ) {
            return 'active';
        }
        return '';
    }
    
    0 讨论(0)
  • 2020-12-04 09:44

    In header.php insert this code to show menu:

    <?php
        wp_nav_menu(
            array(
                'theme_location' => '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 '<li'.$is_current_item.'><a href="'.$item->url.'">'.$item->title;
        }
    
        function end_el( &$output, $item, $depth = 0, $args = array() ) {
            echo '</a></li>';
        }
    }
    
    0 讨论(0)
提交回复
热议问题