Walker_Nav_Menu

Core class used to implement an HTML list of nav menu items.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-walker-nav-menu.php  
  1. class Walker_Nav_Menu extends Walker { 
  2. /** 
  3. * What the class handles. 
  4. * @since 3.0.0 
  5. * @access public 
  6. * @var string 
  7. * @see Walker::$tree_type 
  8. */ 
  9. public $tree_type = array( 'post_type', 'taxonomy', 'custom' ); 
  10.  
  11. /** 
  12. * Database fields to use. 
  13. * @since 3.0.0 
  14. * @access public 
  15. * @todo Decouple this. 
  16. * @var array 
  17. * @see Walker::$db_fields 
  18. */ 
  19. public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' ); 
  20.  
  21. /** 
  22. * Starts the list before the elements are added. 
  23. * @since 3.0.0 
  24. * @see Walker::start_lvl() 
  25. * @param string $output Passed by reference. Used to append additional content. 
  26. * @param int $depth Depth of menu item. Used for padding. 
  27. * @param array $args An array of wp_nav_menu() arguments. 
  28. */ 
  29. public function start_lvl( &$output, $depth = 0, $args = array() ) { 
  30. $indent = str_repeat("\t", $depth); 
  31. $output .= "\n$indent<ul class=\"sub-menu\">\n"; 
  32.  
  33. /** 
  34. * Ends the list of after the elements are added. 
  35. * @since 3.0.0 
  36. * @see Walker::end_lvl() 
  37. * @param string $output Passed by reference. Used to append additional content. 
  38. * @param int $depth Depth of menu item. Used for padding. 
  39. * @param array $args An array of wp_nav_menu() arguments. 
  40. */ 
  41. public function end_lvl( &$output, $depth = 0, $args = array() ) { 
  42. $indent = str_repeat("\t", $depth); 
  43. $output .= "$indent</ul>\n"; 
  44.  
  45. /** 
  46. * Starts the element output. 
  47. * @since 3.0.0 
  48. * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added. 
  49. * @see Walker::start_el() 
  50. * @param string $output Passed by reference. Used to append additional content. 
  51. * @param object $item Menu item data object. 
  52. * @param int $depth Depth of menu item. Used for padding. 
  53. * @param array $args An array of wp_nav_menu() arguments. 
  54. * @param int $id Current item ID. 
  55. */ 
  56. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { 
  57. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; 
  58.  
  59. $classes = empty( $item->classes ) ? array() : (array) $item->classes; 
  60. $classes[] = 'menu-item-' . $item->ID; 
  61.  
  62. /** 
  63. * Filters the arguments for a single nav menu item. 
  64. * @since 4.4.0 
  65. * @param array $args An array of arguments. 
  66. * @param object $item Menu item data object. 
  67. * @param int $depth Depth of menu item. Used for padding. 
  68. */ 
  69. $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth ); 
  70.  
  71. /** 
  72. * Filters the CSS class(es) applied to a menu item's list item element. 
  73. * @since 3.0.0 
  74. * @since 4.1.0 The `$depth` parameter was added. 
  75. * @param array $classes The CSS classes that are applied to the menu item's `<li>` element. 
  76. * @param object $item The current menu item. 
  77. * @param array $args An array of wp_nav_menu() arguments. 
  78. * @param int $depth Depth of menu item. Used for padding. 
  79. */ 
  80. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) ); 
  81. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; 
  82.  
  83. /** 
  84. * Filters the ID applied to a menu item's list item element. 
  85. * @since 3.0.1 
  86. * @since 4.1.0 The `$depth` parameter was added. 
  87. * @param string $menu_id The ID that is applied to the menu item's `<li>` element. 
  88. * @param object $item The current menu item. 
  89. * @param array $args An array of wp_nav_menu() arguments. 
  90. * @param int $depth Depth of menu item. Used for padding. 
  91. */ 
  92. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth ); 
  93. $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; 
  94.  
  95. $output .= $indent . '<li' . $id . $class_names .'>'; 
  96.  
  97. $atts = array(); 
  98. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; 
  99. $atts['target'] = ! empty( $item->target ) ? $item->target : ''; 
  100. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; 
  101. $atts['href'] = ! empty( $item->url ) ? $item->url : ''; 
  102.  
  103. /** 
  104. * Filters the HTML attributes applied to a menu item's anchor element. 
  105. * @since 3.6.0 
  106. * @since 4.1.0 The `$depth` parameter was added. 
  107. * @param array $atts { 
  108. * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored. 
  109. * @type string $title Title attribute. 
  110. * @type string $target Target attribute. 
  111. * @type string $rel The rel attribute. 
  112. * @type string $href The href attribute. 
  113. * } 
  114. * @param object $item The current menu item. 
  115. * @param array $args An array of wp_nav_menu() arguments. 
  116. * @param int $depth Depth of menu item. Used for padding. 
  117. */ 
  118. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth ); 
  119.  
  120. $attributes = ''; 
  121. foreach ( $atts as $attr => $value ) { 
  122. if ( ! empty( $value ) ) { 
  123. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); 
  124. $attributes .= ' ' . $attr . '="' . $value . '"'; 
  125.  
  126. /** This filter is documented in wp-includes/post-template.php */ 
  127. $title = apply_filters( 'the_title', $item->title, $item->ID ); 
  128.  
  129. /** 
  130. * Filters a menu item's title. 
  131. * @since 4.4.0 
  132. * @param string $title The menu item's title. 
  133. * @param object $item The current menu item. 
  134. * @param array $args An array of wp_nav_menu() arguments. 
  135. * @param int $depth Depth of menu item. Used for padding. 
  136. */ 
  137. $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth ); 
  138.  
  139. $item_output = $args->before; 
  140. $item_output .= '<a'. $attributes .'>'; 
  141. $item_output .= $args->link_before . $title . $args->link_after; 
  142. $item_output .= '</a>'; 
  143. $item_output .= $args->after; 
  144.  
  145. /** 
  146. * Filters a menu item's starting output. 
  147. * The menu item's starting output only includes `$args->before`, the opening `<a>`,  
  148. * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is 
  149. * no filter for modifying the opening and closing `<li>` for a menu item. 
  150. * @since 3.0.0 
  151. * @param string $item_output The menu item's starting HTML output. 
  152. * @param object $item Menu item data object. 
  153. * @param int $depth Depth of menu item. Used for padding. 
  154. * @param array $args An array of wp_nav_menu() arguments. 
  155. */ 
  156. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); 
  157.  
  158. /** 
  159. * Ends the element output, if needed. 
  160. * @since 3.0.0 
  161. * @see Walker::end_el() 
  162. * @param string $output Passed by reference. Used to append additional content. 
  163. * @param object $item Page data object. Not used. 
  164. * @param int $depth Depth of page. Not Used. 
  165. * @param array $args An array of wp_nav_menu() arguments. 
  166. */ 
  167. public function end_el( &$output, $item, $depth = 0, $args = array() ) { 
  168. $output .= "</li>\n"; 
  169.  
  170. } // Walker_Nav_Menu