• Hey @all,

    i have a problem with the wp_nav_menu. I will add a new class when the menu point is active

    For example:
    This is the standard menupoint:

    <li ><a href="#"><span class="sliding-middle-out">Menu1</span></a></li>';

    And when the menu active i need a other class
    <li ><a href="#"><span class="activeitem">Menu1</span></a></li>

    Here is the walker (i found this in inet and change it)

    class My_Nav_Menu_Walker extends Walker_Nav_Menu
    {
        function start_el(&$output, $item, $depth, $args)
        {
            global $wp_query;
    
            $class_names = $value = '';
    
            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
            $class = in_array('current-menu-item', $classes) ? ' class="active"' : '';
    
            $output .= '<li>';
    
            $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
            $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
            $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
            $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
    
            $item_description = !empty($item->description) ? esc_attr($item->description) : '';
    
            $item_output = $args->before;
            $item_output .= '<a'.$class. $attributes .'><span class="activmenu">';
            $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
            #$item_output .= '<span>'.$item_description.'</span>';
            $item_output .= '</span></a>';
            $item_output .= $args->after;
    
            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
        }
    }
  • The topic ‘Customize the nav menu – walker add a new – class when activ’ is closed to new replies.