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 stdClass $args An object of wp_nav_menu() arguments. 
  28. */ 
  29. public function start_lvl( &$output, $depth = 0, $args = array() ) { 
  30. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { 
  31. $t = ''; 
  32. $n = ''; 
  33. } else { 
  34. $t = "\t"; 
  35. $n = "\n"; 
  36. $indent = str_repeat( $t, $depth ); 
  37. $output .= "{$n}{$indent}<ul class=\"sub-menu\">{$n}"; 
  38.  
  39. /** 
  40. * Ends the list of after the elements are added. 
  41. * @since 3.0.0 
  42. * @see Walker::end_lvl() 
  43. * @param string $output Passed by reference. Used to append additional content. 
  44. * @param int $depth Depth of menu item. Used for padding. 
  45. * @param stdClass $args An object of wp_nav_menu() arguments. 
  46. */ 
  47. public function end_lvl( &$output, $depth = 0, $args = array() ) { 
  48. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { 
  49. $t = ''; 
  50. $n = ''; 
  51. } else { 
  52. $t = "\t"; 
  53. $n = "\n"; 
  54. $indent = str_repeat( $t, $depth ); 
  55. $output .= "$indent</ul>{$n}"; 
  56.  
  57. /** 
  58. * Starts the element output. 
  59. * @since 3.0.0 
  60. * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added. 
  61. * @see Walker::start_el() 
  62. * @param string $output Passed by reference. Used to append additional content. 
  63. * @param WP_Post $item Menu item data object. 
  64. * @param int $depth Depth of menu item. Used for padding. 
  65. * @param stdClass $args An object of wp_nav_menu() arguments. 
  66. * @param int $id Current item ID. 
  67. */ 
  68. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { 
  69. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { 
  70. $t = ''; 
  71. $n = ''; 
  72. } else { 
  73. $t = "\t"; 
  74. $n = "\n"; 
  75. $indent = ( $depth ) ? str_repeat( $t, $depth ) : ''; 
  76.  
  77. $classes = empty( $item->classes ) ? array() : (array) $item->classes; 
  78. $classes[] = 'menu-item-' . $item->ID; 
  79.  
  80. /** 
  81. * Filters the arguments for a single nav menu item. 
  82. * @since 4.4.0 
  83. * @param stdClass $args An object of wp_nav_menu() arguments. 
  84. * @param WP_Post $item Menu item data object. 
  85. * @param int $depth Depth of menu item. Used for padding. 
  86. */ 
  87. $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth ); 
  88.  
  89. /** 
  90. * Filters the CSS class(es) applied to a menu item's list item element. 
  91. * @since 3.0.0 
  92. * @since 4.1.0 The `$depth` parameter was added. 
  93. * @param array $classes The CSS classes that are applied to the menu item's `<li>` element. 
  94. * @param WP_Post $item The current menu item. 
  95. * @param stdClass $args An object of wp_nav_menu() arguments. 
  96. * @param int $depth Depth of menu item. Used for padding. 
  97. */ 
  98. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) ); 
  99. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; 
  100.  
  101. /** 
  102. * Filters the ID applied to a menu item's list item element. 
  103. * @since 3.0.1 
  104. * @since 4.1.0 The `$depth` parameter was added. 
  105. * @param string $menu_id The ID that is applied to the menu item's `<li>` element. 
  106. * @param WP_Post $item The current menu item. 
  107. * @param stdClass $args An object of wp_nav_menu() arguments. 
  108. * @param int $depth Depth of menu item. Used for padding. 
  109. */ 
  110. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth ); 
  111. $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; 
  112.  
  113. $output .= $indent . '<li' . $id . $class_names .'>'; 
  114.  
  115. $atts = array(); 
  116. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; 
  117. $atts['target'] = ! empty( $item->target ) ? $item->target : ''; 
  118. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; 
  119. $atts['href'] = ! empty( $item->url ) ? $item->url : ''; 
  120.  
  121. /** 
  122. * Filters the HTML attributes applied to a menu item's anchor element. 
  123. * @since 3.6.0 
  124. * @since 4.1.0 The `$depth` parameter was added. 
  125. * @param array $atts { 
  126. * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored. 
  127. * @type string $title Title attribute. 
  128. * @type string $target Target attribute. 
  129. * @type string $rel The rel attribute. 
  130. * @type string $href The href attribute. 
  131. * } 
  132. * @param WP_Post $item The current menu item. 
  133. * @param stdClass $args An object of wp_nav_menu() arguments. 
  134. * @param int $depth Depth of menu item. Used for padding. 
  135. */ 
  136. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth ); 
  137.  
  138. $attributes = ''; 
  139. foreach ( $atts as $attr => $value ) { 
  140. if ( ! empty( $value ) ) { 
  141. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); 
  142. $attributes .= ' ' . $attr . '="' . $value . '"'; 
  143.  
  144. /** This filter is documented in wp-includes/post-template.php */ 
  145. $title = apply_filters( 'the_title', $item->title, $item->ID ); 
  146.  
  147. /** 
  148. * Filters a menu item's title. 
  149. * @since 4.4.0 
  150. * @param string $title The menu item's title. 
  151. * @param WP_Post $item The current menu item. 
  152. * @param stdClass $args An object of wp_nav_menu() arguments. 
  153. * @param int $depth Depth of menu item. Used for padding. 
  154. */ 
  155. $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth ); 
  156.  
  157. $item_output = $args->before; 
  158. $item_output .= '<a'. $attributes .'>'; 
  159. $item_output .= $args->link_before . $title . $args->link_after; 
  160. $item_output .= '</a>'; 
  161. $item_output .= $args->after; 
  162.  
  163. /** 
  164. * Filters a menu item's starting output. 
  165. * The menu item's starting output only includes `$args->before`, the opening `<a>`,  
  166. * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is 
  167. * no filter for modifying the opening and closing `<li>` for a menu item. 
  168. * @since 3.0.0 
  169. * @param string $item_output The menu item's starting HTML output. 
  170. * @param WP_Post $item Menu item data object. 
  171. * @param int $depth Depth of menu item. Used for padding. 
  172. * @param stdClass $args An object of wp_nav_menu() arguments. 
  173. */ 
  174. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); 
  175.  
  176. /** 
  177. * Ends the element output, if needed. 
  178. * @since 3.0.0 
  179. * @see Walker::end_el() 
  180. * @param string $output Passed by reference. Used to append additional content. 
  181. * @param WP_Post $item Page data object. Not used. 
  182. * @param int $depth Depth of page. Not Used. 
  183. * @param stdClass $args An object of wp_nav_menu() arguments. 
  184. */ 
  185. public function end_el( &$output, $item, $depth = 0, $args = array() ) { 
  186. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { 
  187. $t = ''; 
  188. $n = ''; 
  189. } else { 
  190. $t = "\t"; 
  191. $n = "\n"; 
  192. $output .= "</li>{$n}"; 
  193.  
  194. } // Walker_Nav_Menu