3
* Navigation Menu template functions
3
* Nav Menu API: Template functions
6
6
* @subpackage Nav_Menus
11
* Create HTML list of nav menu items.
16
class Walker_Nav_Menu extends Walker {
18
* What the class handles.
20
* @see Walker::$tree_type
24
public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
27
* Database fields to use.
29
* @see Walker::$db_fields
31
* @todo Decouple this.
34
public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
37
* Starts the list before the elements are added.
39
* @see Walker::start_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 array $args An array of arguments. @see wp_nav_menu()
47
public function start_lvl( &$output, $depth = 0, $args = array() ) {
48
$indent = str_repeat("\t", $depth);
49
$output .= "\n$indent<ul class=\"sub-menu\">\n";
53
* Ends the list of after the elements are added.
55
* @see Walker::end_lvl()
59
* @param string $output Passed by reference. Used to append additional content.
60
* @param int $depth Depth of menu item. Used for padding.
61
* @param array $args An array of arguments. @see wp_nav_menu()
63
public function end_lvl( &$output, $depth = 0, $args = array() ) {
64
$indent = str_repeat("\t", $depth);
65
$output .= "$indent</ul>\n";
69
* Start the element output.
71
* @see Walker::start_el()
74
* @since 4.4.0 'nav_menu_item_args' filter was added.
76
* @param string $output Passed by reference. Used to append additional content.
77
* @param object $item Menu item data object.
78
* @param int $depth Depth of menu item. Used for padding.
79
* @param array $args An array of arguments. @see wp_nav_menu()
80
* @param int $id Current item ID.
82
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
83
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
85
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
86
$classes[] = 'menu-item-' . $item->ID;
89
* Filter the arguments for a single nav menu item.
93
* @param array $args An array of arguments.
94
* @param object $item Menu item data object.
95
* @param int $depth Depth of menu item. Used for padding.
97
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
100
* Filter the CSS class(es) applied to a menu item's list item element.
103
* @since 4.1.0 The `$depth` parameter was added.
105
* @param array $classes The CSS classes that are applied to the menu item's `<li>` element.
106
* @param object $item The current menu item.
107
* @param array $args An array of {@see wp_nav_menu()} arguments.
108
* @param int $depth Depth of menu item. Used for padding.
110
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
111
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
114
* Filter the ID applied to a menu item's list item element.
117
* @since 4.1.0 The `$depth` parameter was added.
119
* @param string $menu_id The ID that is applied to the menu item's `<li>` element.
120
* @param object $item The current menu item.
121
* @param array $args An array of {@see wp_nav_menu()} arguments.
122
* @param int $depth Depth of menu item. Used for padding.
124
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
125
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
127
$output .= $indent . '<li' . $id . $class_names .'>';
130
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
131
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
132
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
133
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
136
* Filter the HTML attributes applied to a menu item's anchor element.
139
* @since 4.1.0 The `$depth` parameter was added.
141
* @param array $atts {
142
* The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
144
* @type string $title Title attribute.
145
* @type string $target Target attribute.
146
* @type string $rel The rel attribute.
147
* @type string $href The href attribute.
149
* @param object $item The current menu item.
150
* @param array $args An array of {@see wp_nav_menu()} arguments.
151
* @param int $depth Depth of menu item. Used for padding.
153
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
156
foreach ( $atts as $attr => $value ) {
157
if ( ! empty( $value ) ) {
158
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
159
$attributes .= ' ' . $attr . '="' . $value . '"';
163
/** This filter is documented in wp-includes/post-template.php */
164
$title = apply_filters( 'the_title', $item->title, $item->ID );
167
* Filter a menu item's title.
171
* @param string $title The menu item's title.
172
* @param object $item The current menu item.
173
* @param array $args An array of {@see wp_nav_menu()} arguments.
174
* @param int $depth Depth of menu item. Used for padding.
176
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
178
$item_output = $args->before;
179
$item_output .= '<a'. $attributes .'>';
180
$item_output .= $args->link_before . $title . $args->link_after;
181
$item_output .= '</a>';
182
$item_output .= $args->after;
185
* Filter a menu item's starting output.
187
* The menu item's starting output only includes `$args->before`, the opening `<a>`,
188
* the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
189
* no filter for modifying the opening and closing `<li>` for a menu item.
193
* @param string $item_output The menu item's starting HTML output.
194
* @param object $item Menu item data object.
195
* @param int $depth Depth of menu item. Used for padding.
196
* @param array $args An array of {@see wp_nav_menu()} arguments.
198
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
202
* Ends the element output, if needed.
204
* @see Walker::end_el()
208
* @param string $output Passed by reference. Used to append additional content.
209
* @param object $item Page data object. Not used.
210
* @param int $depth Depth of page. Not Used.
211
* @param array $args An array of arguments. @see wp_nav_menu()
213
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
214
$output .= "</li>\n";
10
/** Walker_Nav_Menu class */
11
require_once ABSPATH . WPINC . '/class-walker-nav-menu.php';
220
14
* Displays a navigation menu.