42613

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.

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.

Answer1:

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.

Answer2:

I have managed to find the solution to this question that seemed to go unanswered for days here - How can I add parent menu description to my Wordpress menu?

Recommend

  • How to print lists of lists of uneven length in Shiny
  • Program does not contain a static 'main' method suitable for entry point
  • Visual studio won't compile template class with *.inl implementation
  • UIView animation continues to run after a new view controller is pushed onto the stack
  • PHP: Get HTTP Protocol Version (HTTP/1.1 vs HTTP/2)
  • How to model a mixture of finite components from different parametric families with JAGS?
  • Sending Content-Type application/x-www-form-urlencoded WSO2 ESB
  • TFS - how do I sum child task hours to parent
  • MeeGo Handset Emulator not starting on Windows 7
  • How to access meteor package name inside package?
  • Why does it draw lines in the wrong place?
  • Updating Dojo provide
  • Display validation errors inside
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Defined variables not working in javascript files when I use getScript
  • Image map in Flex
  • pillow imaging ImportError
  • How can I display the parent menu item's description using Wordpress walkers?
  • Android application: how to use the camera and grab the image bytes?
  • How to view images from protected folder with php?
  • How does document.ready work with angular element directives?
  • Using a canvas object in a thread to do simple animations - Java
  • Is playing sound in Javascript performance heavy?
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Python urlparse: small issue
  • FileReader+canvas image loading problem
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Insert into database using onclick function
  • Azure Cloud Service Web Role web pages do not load
  • Fill an image in a square container while keeping aspect ratio
  • Knitr HTML Loop - Some HTML output, some R output
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Arrays break string types in Julia
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • python draw pie shapes with colour filled
  • Reading document lines to the user (python)