Add Description To the Menu Using wp_nav_menu


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // This goes in function.php
  2.  
  3. class My_Walker extends Walker_Nav_Menu
  4. {
  5. function start_el(&$output, $item, $depth, $args) {
  6. global $wp_query;
  7. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  8.  
  9. $class_names = $value = '';
  10.  
  11. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  12.  
  13. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  14. $class_names = '';
  15.  
  16. $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  17.  
  18. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  19. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  20. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  21. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  22.  
  23. $item_output = $args->before;
  24. $item_output .= '<a'. $attributes .'>';
  25. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  26. $item_output .= '<span>' . $item->description . '</span>';
  27. $item_output .= '</a>';
  28. $item_output .= $args->after;
  29.  
  30. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  31. }
  32. }
  33.  
  34. // Then Display the Menu anywhere
  35.  
  36. <?php
  37. $walker = new My_Walker;
  38. wp_nav_menu( array(
  39. 'container_id' => 'mainmenu',
  40. 'menu_class' => 'sf-menu',
  41. 'walker' => $walker
  42. ));
  43. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.