How can I display the parent menu item’s description using WordPress walkers?

Is there any way to pass a variable from start_el to start_lvl? I want to place the menu description in the wrapper of the submenu items.

class submenu_walker extends Walker_Nav_Menu
{
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("t", $depth);
        $output .= "n$indent<ul class='sub-menu'><div class='menu-image-container'><div class='menu-image'></div></div>n";
    }
    function end_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("t", $depth);
        $output .= "$indent<div class='clear'></div></ul>n";
    }
}

I’ve tried to store the description as a variable in start_el, and access it using global in start_lvl… but it returns nothing.

Read More

I’m really desperate for help on this, as so far I’ve had no results, or responses to this question in terms of answers.

Can someone please help me with this? There are definitely some of you who know how to use the WordPress walker menu.

Related posts

Leave a Reply

2 comments

  1. Here is the way to use this code and it will display the parent description.
    Steps for using of it.
    1: Copy the below class and past into the functions.php file.
    2: Call the menu like

     wp_nav_menu(array(
          'menu_id'=>'',
          'menu_class'=>'',
          'container'=>'',
          'theme_location'=>'#enter theme location#',
          'walker'=> new customize_menu_walker()
        ));
    <br/>
    

    3:See the result.

    class customize_menu_walker extends Walker_Nav_Menu
    {
     function start_el(&$output, $item, $depth, $args)
          {
               global $wp_query;
               $indent = ( $depth ) ? str_repeat( "t", $depth ) : '';
    
               $class_names = $value = '';
    
              // $dbclasses=$item->classes;
               $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    
               $dbclasses=$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
               $class_names = ' class="'. esc_attr( $class_names ) . '"';
    
               $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_output = $args->before;
    
                 //specially for two menu
                 //specially for two menu
               //convert string to array
               $dbclassesArr = explode(" ",$dbclasses);
    
               $item_output .= '<a'. $attributes .'>';
    
               if(in_array("menu-item-has-children", $dbclassesArr)){
                 $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';//description display here.
               }
    
                $item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
                $item_output .= $description.$args->link_after;
                $item_output .= ' '.'</a>';
                $item_output .= $args->after;
    
                $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
                }
    }
    

    Now after updating this function, your menu will show the parent menu description.
    Here is the code.

    Thanking you.