3 * Nav Menu API: Walker_Nav_Menu class
6 * @subpackage Nav_Menus
11 * Core class used to implement an HTML list of nav menu items.
17 class Walker_Nav_Menu extends Walker {
19 * What the class handles.
25 * @see Walker::$tree_type
27 public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
30 * Database fields to use.
34 * @todo Decouple this.
37 * @see Walker::$db_fields
39 public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
42 * Starts the list before the elements are added.
46 * @see Walker::start_lvl()
48 * @param string $output Passed by reference. Used to append additional content.
49 * @param int $depth Depth of menu item. Used for padding.
50 * @param stdClass $args An object of wp_nav_menu() arguments.
52 public function start_lvl( &$output, $depth = 0, $args = array() ) {
53 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
60 $indent = str_repeat( $t, $depth );
61 $output .= "{$n}{$indent}<ul class=\"sub-menu\">{$n}";
65 * Ends the list of after the elements are added.
69 * @see Walker::end_lvl()
71 * @param string $output Passed by reference. Used to append additional content.
72 * @param int $depth Depth of menu item. Used for padding.
73 * @param stdClass $args An object of wp_nav_menu() arguments.
75 public function end_lvl( &$output, $depth = 0, $args = array() ) {
76 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
83 $indent = str_repeat( $t, $depth );
84 $output .= "$indent</ul>{$n}";
88 * Starts the element output.
91 * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
93 * @see Walker::start_el()
95 * @param string $output Passed by reference. Used to append additional content.
96 * @param WP_Post $item Menu item data object.
97 * @param int $depth Depth of menu item. Used for padding.
98 * @param stdClass $args An object of wp_nav_menu() arguments.
99 * @param int $id Current item ID.
101 public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
102 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
109 $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
111 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
112 $classes[] = 'menu-item-' . $item->ID;
115 * Filters the arguments for a single nav menu item.
119 * @param stdClass $args An object of wp_nav_menu() arguments.
120 * @param WP_Post $item Menu item data object.
121 * @param int $depth Depth of menu item. Used for padding.
123 $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
126 * Filters the CSS class(es) applied to a menu item's list item element.
129 * @since 4.1.0 The `$depth` parameter was added.
131 * @param array $classes The CSS classes that are applied to the menu item's `<li>` element.
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.
136 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
137 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
140 * Filters the ID applied to a menu item's list item element.
143 * @since 4.1.0 The `$depth` parameter was added.
145 * @param string $menu_id The ID that is applied to the menu item's `<li>` element.
146 * @param WP_Post $item The current menu item.
147 * @param stdClass $args An object of wp_nav_menu() arguments.
148 * @param int $depth Depth of menu item. Used for padding.
150 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
151 $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
153 $output .= $indent . '<li' . $id . $class_names .'>';
156 $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
157 $atts['target'] = ! empty( $item->target ) ? $item->target : '';
158 $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
159 $atts['href'] = ! empty( $item->url ) ? $item->url : '';
162 * Filters the HTML attributes applied to a menu item's anchor element.
165 * @since 4.1.0 The `$depth` parameter was added.
167 * @param array $atts {
168 * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
170 * @type string $title Title attribute.
171 * @type string $target Target attribute.
172 * @type string $rel The rel attribute.
173 * @type string $href The href attribute.
175 * @param WP_Post $item The current menu item.
176 * @param stdClass $args An object of wp_nav_menu() arguments.
177 * @param int $depth Depth of menu item. Used for padding.
179 $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
182 foreach ( $atts as $attr => $value ) {
183 if ( ! empty( $value ) ) {
184 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
185 $attributes .= ' ' . $attr . '="' . $value . '"';
189 /** This filter is documented in wp-includes/post-template.php */
190 $title = apply_filters( 'the_title', $item->title, $item->ID );
193 * Filters a menu item's title.
197 * @param string $title The menu item's title.
198 * @param WP_Post $item The current menu item.
199 * @param stdClass $args An object of wp_nav_menu() arguments.
200 * @param int $depth Depth of menu item. Used for padding.
202 $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
204 $item_output = $args->before;
205 $item_output .= '<a'. $attributes .'>';
206 $item_output .= $args->link_before . $title . $args->link_after;
207 $item_output .= '</a>';
208 $item_output .= $args->after;
211 * Filters a menu item's starting output.
213 * The menu item's starting output only includes `$args->before`, the opening `<a>`,
214 * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
215 * no filter for modifying the opening and closing `<li>` for a menu item.
219 * @param string $item_output The menu item's starting HTML output.
220 * @param WP_Post $item Menu item data object.
221 * @param int $depth Depth of menu item. Used for padding.
222 * @param stdClass $args An object of wp_nav_menu() arguments.
224 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
228 * Ends the element output, if needed.
232 * @see Walker::end_el()
234 * @param string $output Passed by reference. Used to append additional content.
235 * @param WP_Post $item Page data object. Not used.
236 * @param int $depth Depth of page. Not Used.
237 * @param stdClass $args An object of wp_nav_menu() arguments.
239 public function end_el( &$output, $item, $depth = 0, $args = array() ) {
240 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
247 $output .= "</li>{$n}";