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 array $args An array of wp_nav_menu() arguments.
52 public function start_lvl( &$output, $depth = 0, $args = array() ) {
53 $indent = str_repeat("\t", $depth);
54 $output .= "\n$indent<ul class=\"sub-menu\">\n";
58 * Ends the list of after the elements are added.
62 * @see Walker::end_lvl()
64 * @param string $output Passed by reference. Used to append additional content.
65 * @param int $depth Depth of menu item. Used for padding.
66 * @param array $args An array of wp_nav_menu() arguments.
68 public function end_lvl( &$output, $depth = 0, $args = array() ) {
69 $indent = str_repeat("\t", $depth);
70 $output .= "$indent</ul>\n";
74 * Starts the element output.
77 * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
79 * @see Walker::start_el()
81 * @param string $output Passed by reference. Used to append additional content.
82 * @param object $item Menu item data object.
83 * @param int $depth Depth of menu item. Used for padding.
84 * @param array $args An array of wp_nav_menu() arguments.
85 * @param int $id Current item ID.
87 public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
88 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
90 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
91 $classes[] = 'menu-item-' . $item->ID;
94 * Filters the arguments for a single nav menu item.
98 * @param array $args An array of arguments.
99 * @param object $item Menu item data object.
100 * @param int $depth Depth of menu item. Used for padding.
102 $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
105 * Filters the CSS class(es) applied to a menu item's list item element.
108 * @since 4.1.0 The `$depth` parameter was added.
110 * @param array $classes The CSS classes that are applied to the menu item's `<li>` element.
111 * @param object $item The current menu item.
112 * @param array $args An array of wp_nav_menu() arguments.
113 * @param int $depth Depth of menu item. Used for padding.
115 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
116 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
119 * Filters the ID applied to a menu item's list item element.
122 * @since 4.1.0 The `$depth` parameter was added.
124 * @param string $menu_id The ID that is applied to the menu item's `<li>` element.
125 * @param object $item The current menu item.
126 * @param array $args An array of wp_nav_menu() arguments.
127 * @param int $depth Depth of menu item. Used for padding.
129 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
130 $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
132 $output .= $indent . '<li' . $id . $class_names .'>';
135 $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
136 $atts['target'] = ! empty( $item->target ) ? $item->target : '';
137 $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
138 $atts['href'] = ! empty( $item->url ) ? $item->url : '';
141 * Filters the HTML attributes applied to a menu item's anchor element.
144 * @since 4.1.0 The `$depth` parameter was added.
146 * @param array $atts {
147 * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
149 * @type string $title Title attribute.
150 * @type string $target Target attribute.
151 * @type string $rel The rel attribute.
152 * @type string $href The href attribute.
154 * @param object $item The current menu item.
155 * @param array $args An array of wp_nav_menu() arguments.
156 * @param int $depth Depth of menu item. Used for padding.
158 $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
161 foreach ( $atts as $attr => $value ) {
162 if ( ! empty( $value ) ) {
163 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
164 $attributes .= ' ' . $attr . '="' . $value . '"';
168 /** This filter is documented in wp-includes/post-template.php */
169 $title = apply_filters( 'the_title', $item->title, $item->ID );
172 * Filters a menu item's title.
176 * @param string $title The menu item's title.
177 * @param object $item The current menu item.
178 * @param array $args An array of wp_nav_menu() arguments.
179 * @param int $depth Depth of menu item. Used for padding.
181 $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
183 $item_output = $args->before;
184 $item_output .= '<a'. $attributes .'>';
185 $item_output .= $args->link_before . $title . $args->link_after;
186 $item_output .= '</a>';
187 $item_output .= $args->after;
190 * Filters a menu item's starting output.
192 * The menu item's starting output only includes `$args->before`, the opening `<a>`,
193 * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
194 * no filter for modifying the opening and closing `<li>` for a menu item.
198 * @param string $item_output The menu item's starting HTML output.
199 * @param object $item Menu item data object.
200 * @param int $depth Depth of menu item. Used for padding.
201 * @param array $args An array of wp_nav_menu() arguments.
203 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
207 * Ends the element output, if needed.
211 * @see Walker::end_el()
213 * @param string $output Passed by reference. Used to append additional content.
214 * @param object $item Page data object. Not used.
215 * @param int $depth Depth of page. Not Used.
216 * @param array $args An array of wp_nav_menu() arguments.
218 public function end_el( &$output, $item, $depth = 0, $args = array() ) {
219 $output .= "</li>\n";