77 * @param object $item Menu item data object. |
76 * @param object $item Menu item data object. |
78 * @param int $depth Depth of menu item. Used for padding. |
77 * @param int $depth Depth of menu item. Used for padding. |
79 * @param array $args An array of arguments. @see wp_nav_menu() |
78 * @param array $args An array of arguments. @see wp_nav_menu() |
80 * @param int $id Current item ID. |
79 * @param int $id Current item ID. |
81 */ |
80 */ |
82 function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
81 public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
83 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; |
82 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; |
84 |
|
85 $class_names = $value = ''; |
|
86 |
83 |
87 $classes = empty( $item->classes ) ? array() : (array) $item->classes; |
84 $classes = empty( $item->classes ) ? array() : (array) $item->classes; |
88 $classes[] = 'menu-item-' . $item->ID; |
85 $classes[] = 'menu-item-' . $item->ID; |
89 |
86 |
90 /** |
87 /** |
91 * Filter the CSS class(es) applied to a menu item's <li>. |
88 * Filter the CSS class(es) applied to a menu item's list item element. |
92 * |
89 * |
93 * @since 3.0.0 |
90 * @since 3.0.0 |
94 * |
91 * @since 4.1.0 The `$depth` parameter was added. |
95 * @param array $classes The CSS classes that are applied to the menu item's <li>. |
92 * |
|
93 * @param array $classes The CSS classes that are applied to the menu item's `<li>` element. |
96 * @param object $item The current menu item. |
94 * @param object $item The current menu item. |
97 * @param array $args An array of arguments. @see wp_nav_menu() |
95 * @param array $args An array of {@see wp_nav_menu()} arguments. |
|
96 * @param int $depth Depth of menu item. Used for padding. |
98 */ |
97 */ |
99 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) ); |
98 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) ); |
100 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
99 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
101 |
100 |
102 /** |
101 /** |
103 * Filter the ID applied to a menu item's <li>. |
102 * Filter the ID applied to a menu item's list item element. |
104 * |
103 * |
105 * @since 3.0.1 |
104 * @since 3.0.1 |
106 * |
105 * @since 4.1.0 The `$depth` parameter was added. |
107 * @param string The ID that is applied to the menu item's <li>. |
106 * |
108 * @param object $item The current menu item. |
107 * @param string $menu_id The ID that is applied to the menu item's `<li>` element. |
109 * @param array $args An array of arguments. @see wp_nav_menu() |
108 * @param object $item The current menu item. |
|
109 * @param array $args An array of {@see wp_nav_menu()} arguments. |
|
110 * @param int $depth Depth of menu item. Used for padding. |
110 */ |
111 */ |
111 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args ); |
112 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth ); |
112 $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; |
113 $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; |
113 |
114 |
114 $output .= $indent . '<li' . $id . $value . $class_names .'>'; |
115 $output .= $indent . '<li' . $id . $class_names .'>'; |
115 |
116 |
116 $atts = array(); |
117 $atts = array(); |
117 $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; |
118 $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; |
118 $atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
119 $atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
119 $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
120 $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
120 $atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
121 $atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
121 |
122 |
122 /** |
123 /** |
123 * Filter the HTML attributes applied to a menu item's <a>. |
124 * Filter the HTML attributes applied to a menu item's anchor element. |
124 * |
125 * |
125 * @since 3.6.0 |
126 * @since 3.6.0 |
|
127 * @since 4.1.0 The `$depth` parameter was added. |
126 * |
128 * |
127 * @param array $atts { |
129 * @param array $atts { |
128 * The HTML attributes applied to the menu item's <a>, empty strings are ignored. |
130 * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored. |
129 * |
131 * |
130 * @type string $title The title attribute. |
132 * @type string $title Title attribute. |
131 * @type string $target The target attribute. |
133 * @type string $target Target attribute. |
132 * @type string $rel The rel attribute. |
134 * @type string $rel The rel attribute. |
133 * @type string $href The href attribute. |
135 * @type string $href The href attribute. |
134 * } |
136 * } |
135 * @param object $item The current menu item. |
137 * @param object $item The current menu item. |
136 * @param array $args An array of arguments. @see wp_nav_menu() |
138 * @param array $args An array of {@see wp_nav_menu()} arguments. |
|
139 * @param int $depth Depth of menu item. Used for padding. |
137 */ |
140 */ |
138 $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args ); |
141 $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth ); |
139 |
142 |
140 $attributes = ''; |
143 $attributes = ''; |
141 foreach ( $atts as $attr => $value ) { |
144 foreach ( $atts as $attr => $value ) { |
142 if ( ! empty( $value ) ) { |
145 if ( ! empty( $value ) ) { |
143 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
146 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
153 $item_output .= $args->after; |
156 $item_output .= $args->after; |
154 |
157 |
155 /** |
158 /** |
156 * Filter a menu item's starting output. |
159 * Filter a menu item's starting output. |
157 * |
160 * |
158 * The menu item's starting output only includes $args->before, the opening <a>, |
161 * The menu item's starting output only includes `$args->before`, the opening `<a>`, |
159 * the menu item's title, the closing </a>, and $args->after. Currently, there is |
162 * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is |
160 * no filter for modifying the opening and closing <li> for a menu item. |
163 * no filter for modifying the opening and closing `<li>` for a menu item. |
161 * |
164 * |
162 * @since 3.0.0 |
165 * @since 3.0.0 |
163 * |
166 * |
164 * @param string $item_output The menu item's starting HTML output. |
167 * @param string $item_output The menu item's starting HTML output. |
165 * @param object $item Menu item data object. |
168 * @param object $item Menu item data object. |
166 * @param int $depth Depth of menu item. Used for padding. |
169 * @param int $depth Depth of menu item. Used for padding. |
167 * @param array $args An array of arguments. @see wp_nav_menu() |
170 * @param array $args An array of {@see wp_nav_menu()} arguments. |
168 */ |
171 */ |
169 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
172 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
170 } |
173 } |
171 |
174 |
172 /** |
175 /** |
179 * @param string $output Passed by reference. Used to append additional content. |
182 * @param string $output Passed by reference. Used to append additional content. |
180 * @param object $item Page data object. Not used. |
183 * @param object $item Page data object. Not used. |
181 * @param int $depth Depth of page. Not Used. |
184 * @param int $depth Depth of page. Not Used. |
182 * @param array $args An array of arguments. @see wp_nav_menu() |
185 * @param array $args An array of arguments. @see wp_nav_menu() |
183 */ |
186 */ |
184 function end_el( &$output, $item, $depth = 0, $args = array() ) { |
187 public function end_el( &$output, $item, $depth = 0, $args = array() ) { |
185 $output .= "</li>\n"; |
188 $output .= "</li>\n"; |
186 } |
189 } |
187 |
190 |
188 } // Walker_Nav_Menu |
191 } // Walker_Nav_Menu |
189 |
192 |
190 /** |
193 /** |
191 * Displays a navigation menu. |
194 * Displays a navigation menu. |
192 * |
195 * |
193 * Optional $args contents: |
|
194 * |
|
195 * menu - The menu that is desired. Accepts (matching in order) id, slug, name. Defaults to blank. |
|
196 * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'. |
|
197 * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented. |
|
198 * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'. |
|
199 * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'. |
|
200 * container_id - The ID that is applied to the container. Defaults to blank. |
|
201 * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'. Set to false for no fallback. |
|
202 * before - Text before the link text. |
|
203 * after - Text after the link text. |
|
204 * link_before - Text before the link. |
|
205 * link_after - Text after the link. |
|
206 * echo - Whether to echo the menu or return it. Defaults to echo. |
|
207 * depth - how many levels of the hierarchy are to be included. 0 means all. Defaults to 0. |
|
208 * walker - allows a custom walker to be specified. |
|
209 * theme_location - the location in the theme to be used. Must be registered with register_nav_menu() in order to be selectable by the user. |
|
210 * items_wrap - How the list items should be wrapped. Defaults to a ul with an id and class. Uses printf() format with numbered placeholders. |
|
211 * |
|
212 * @since 3.0.0 |
196 * @since 3.0.0 |
213 * |
197 * |
214 * @param array $args Arguments |
198 * @param array $args { |
|
199 * Optional. Array of nav menu arguments. |
|
200 * |
|
201 * @type string $menu Desired menu. Accepts (matching in order) id, slug, name. Default empty. |
|
202 * @type string $menu_class CSS class to use for the ul element which forms the menu. Default 'menu'. |
|
203 * @type string $menu_id The ID that is applied to the ul element which forms the menu. |
|
204 * Default is the menu slug, incremented. |
|
205 * @type string $container Whether to wrap the ul, and what to wrap it with. Default 'div'. |
|
206 * @type string $container_class Class that is applied to the container. Default 'menu-{menu slug}-container'. |
|
207 * @type string $container_id The ID that is applied to the container. Default empty. |
|
208 * @type callback|bool $fallback_cb If the menu doesn't exists, a callback function will fire. |
|
209 * Default is 'wp_page_menu'. Set to false for no fallback. |
|
210 * @type string $before Text before the link text. Default empty. |
|
211 * @type string $after Text after the link text. Default empty. |
|
212 * @type string $link_before Text before the link. Default empty. |
|
213 * @type string $link_after Text after the link. Default empty. |
|
214 * @type bool $echo Whether to echo the menu or return it. Default true. |
|
215 * @type int $depth How many levels of the hierarchy are to be included. 0 means all. Default 0. |
|
216 * @type object $walker Instance of a custom walker class. Default empty. |
|
217 * @type string $theme_location Theme location to be used. Must be registered with register_nav_menu() |
|
218 * in order to be selectable by the user. |
|
219 * @type string $items_wrap How the list items should be wrapped. Default is a ul with an id and class. |
|
220 * Uses printf() format with numbered placeholders. |
|
221 * } |
|
222 * @return mixed Menu output if $echo is false, false if there are no items or no menu was found. |
215 */ |
223 */ |
216 function wp_nav_menu( $args = array() ) { |
224 function wp_nav_menu( $args = array() ) { |
217 static $menu_id_slugs = array(); |
225 static $menu_id_slugs = array(); |
218 |
226 |
219 $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', |
227 $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', |
224 /** |
232 /** |
225 * Filter the arguments used to display a navigation menu. |
233 * Filter the arguments used to display a navigation menu. |
226 * |
234 * |
227 * @since 3.0.0 |
235 * @since 3.0.0 |
228 * |
236 * |
229 * @param array $args Arguments from {@see wp_nav_menu()}. |
237 * @see wp_nav_menu() |
|
238 * |
|
239 * @param array $args Array of wp_nav_menu() arguments. |
230 */ |
240 */ |
231 $args = apply_filters( 'wp_nav_menu_args', $args ); |
241 $args = apply_filters( 'wp_nav_menu_args', $args ); |
232 $args = (object) $args; |
242 $args = (object) $args; |
|
243 |
|
244 /** |
|
245 * Filter whether to short-circuit the wp_nav_menu() output. |
|
246 * |
|
247 * Returning a non-null value to the filter will short-circuit |
|
248 * wp_nav_menu(), echoing that value if $args->echo is true, |
|
249 * returning that value otherwise. |
|
250 * |
|
251 * @since 3.9.0 |
|
252 * |
|
253 * @see wp_nav_menu() |
|
254 * |
|
255 * @param string|null $output Nav menu output to short-circuit with. Default null. |
|
256 * @param object $args An object containing wp_nav_menu() arguments. |
|
257 */ |
|
258 $nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args ); |
|
259 |
|
260 if ( null !== $nav_menu ) { |
|
261 if ( $args->echo ) { |
|
262 echo $nav_menu; |
|
263 return; |
|
264 } |
|
265 |
|
266 return $nav_menu; |
|
267 } |
233 |
268 |
234 // Get the nav menu based on the requested menu |
269 // Get the nav menu based on the requested menu |
235 $menu = wp_get_nav_menu_object( $args->menu ); |
270 $menu = wp_get_nav_menu_object( $args->menu ); |
236 |
271 |
237 // Get the nav menu based on the theme_location |
272 // Get the nav menu based on the theme_location |
339 /** |
376 /** |
340 * Filter the HTML list content for navigation menus. |
377 * Filter the HTML list content for navigation menus. |
341 * |
378 * |
342 * @since 3.0.0 |
379 * @since 3.0.0 |
343 * |
380 * |
|
381 * @see wp_nav_menu() |
|
382 * |
344 * @param string $items The HTML list content for the menu items. |
383 * @param string $items The HTML list content for the menu items. |
345 * @param array $args Arguments from {@see wp_nav_menu()}. |
384 * @param object $args An object containing wp_nav_menu() arguments. |
346 */ |
385 */ |
347 $items = apply_filters( 'wp_nav_menu_items', $items, $args ); |
386 $items = apply_filters( 'wp_nav_menu_items', $items, $args ); |
348 /** |
387 /** |
349 * Filter the HTML list content for a specific navigation menu. |
388 * Filter the HTML list content for a specific navigation menu. |
350 * |
389 * |
351 * @since 3.0.0 |
390 * @since 3.0.0 |
352 * |
391 * |
|
392 * @see wp_nav_menu() |
|
393 * |
353 * @param string $items The HTML list content for the menu items. |
394 * @param string $items The HTML list content for the menu items. |
354 * @param array $args Arguments from {@see wp_nav_menu()}. |
395 * @param object $args An object containing wp_nav_menu() arguments. |
355 */ |
396 */ |
356 $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args ); |
397 $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args ); |
357 |
398 |
358 // Don't print any markup if there are no items at this point. |
399 // Don't print any markup if there are no items at this point. |
359 if ( empty( $items ) ) |
400 if ( empty( $items ) ) |