3 * Nav Menu API: Template functions
6 * @subpackage Nav_Menus
10 /** Walker_Nav_Menu class */
11 require_once ABSPATH . WPINC . '/class-walker-nav-menu.php';
14 * Displays a navigation menu.
18 * @staticvar array $menu_id_slugs
20 * @param array $args {
21 * Optional. Array of nav menu arguments.
23 * @type string $menu Desired menu. Accepts (matching in order) id, slug, name. Default empty.
24 * @type string $menu_class CSS class to use for the ul element which forms the menu. Default 'menu'.
25 * @type string $menu_id The ID that is applied to the ul element which forms the menu.
26 * Default is the menu slug, incremented.
27 * @type string $container Whether to wrap the ul, and what to wrap it with. Default 'div'.
28 * @type string $container_class Class that is applied to the container. Default 'menu-{menu slug}-container'.
29 * @type string $container_id The ID that is applied to the container. Default empty.
30 * @type callable|bool $fallback_cb If the menu doesn't exists, a callback function will fire.
31 * Default is 'wp_page_menu'. Set to false for no fallback.
32 * @type string $before Text before the link markup. Default empty.
33 * @type string $after Text after the link markup. Default empty.
34 * @type string $link_before Text before the link text. Default empty.
35 * @type string $link_after Text after the link text. Default empty.
36 * @type bool $echo Whether to echo the menu or return it. Default true.
37 * @type int $depth How many levels of the hierarchy are to be included. 0 means all. Default 0.
38 * @type object $walker Instance of a custom walker class. Default empty.
39 * @type string $theme_location Theme location to be used. Must be registered with register_nav_menu()
40 * in order to be selectable by the user.
41 * @type string $items_wrap How the list items should be wrapped. Default is a ul with an id and class.
42 * Uses printf() format with numbered placeholders.
44 * @return object|false|void Menu output if $echo is false, false if there are no items or no menu was found.
46 function wp_nav_menu( $args = array() ) {
47 static $menu_id_slugs = array();
49 $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
50 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
51 'depth' => 0, 'walker' => '', 'theme_location' => '' );
53 $args = wp_parse_args( $args, $defaults );
55 * Filters the arguments used to display a navigation menu.
61 * @param array $args Array of wp_nav_menu() arguments.
63 $args = apply_filters( 'wp_nav_menu_args', $args );
64 $args = (object) $args;
67 * Filters whether to short-circuit the wp_nav_menu() output.
69 * Returning a non-null value to the filter will short-circuit
70 * wp_nav_menu(), echoing that value if $args->echo is true,
71 * returning that value otherwise.
77 * @param string|null $output Nav menu output to short-circuit with. Default null.
78 * @param object $args An object containing wp_nav_menu() arguments.
80 $nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args );
82 if ( null !== $nav_menu ) {
91 // Get the nav menu based on the requested menu
92 $menu = wp_get_nav_menu_object( $args->menu );
94 // Get the nav menu based on the theme_location
95 if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
96 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
98 // get the first menu that has items if we still can't find a menu
99 if ( ! $menu && !$args->theme_location ) {
100 $menus = wp_get_nav_menus();
101 foreach ( $menus as $menu_maybe ) {
102 if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) {
109 if ( empty( $args->menu ) ) {
113 // If the menu exists, get its items.
114 if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
115 $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
118 * If no menu was found:
119 * - Fall back (if one was specified), or bail.
121 * If no menu items were found:
122 * - Fall back, but only if no theme location was specified.
125 if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
126 && isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) )
127 return call_user_func( $args->fallback_cb, (array) $args );
129 if ( ! $menu || is_wp_error( $menu ) )
132 $nav_menu = $items = '';
134 $show_container = false;
135 if ( $args->container ) {
137 * Filters the list of HTML tags that are valid for use as menu containers.
141 * @param array $tags The acceptable HTML tags for use as menu containers.
142 * Default is array containing 'div' and 'nav'.
144 $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
145 if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags ) ) {
146 $show_container = true;
147 $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
148 $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
149 $nav_menu .= '<'. $args->container . $id . $class . '>';
153 // Set up the $menu_item variables
154 _wp_menu_item_classes_by_context( $menu_items );
156 $sorted_menu_items = $menu_items_with_children = array();
157 foreach ( (array) $menu_items as $menu_item ) {
158 $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
159 if ( $menu_item->menu_item_parent )
160 $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
163 // Add the menu-item-has-children class where applicable
164 if ( $menu_items_with_children ) {
165 foreach ( $sorted_menu_items as &$menu_item ) {
166 if ( isset( $menu_items_with_children[ $menu_item->ID ] ) )
167 $menu_item->classes[] = 'menu-item-has-children';
171 unset( $menu_items, $menu_item );
174 * Filters the sorted list of menu item objects before generating the menu's HTML.
178 * @param array $sorted_menu_items The menu items, sorted by each menu item's menu order.
179 * @param object $args An object containing wp_nav_menu() arguments.
181 $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
183 $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
184 unset($sorted_menu_items);
187 if ( ! empty( $args->menu_id ) ) {
188 $wrap_id = $args->menu_id;
190 $wrap_id = 'menu-' . $menu->slug;
191 while ( in_array( $wrap_id, $menu_id_slugs ) ) {
192 if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
193 $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
195 $wrap_id = $wrap_id . '-1';
198 $menu_id_slugs[] = $wrap_id;
200 $wrap_class = $args->menu_class ? $args->menu_class : '';
203 * Filters the HTML list content for navigation menus.
209 * @param string $items The HTML list content for the menu items.
210 * @param object $args An object containing wp_nav_menu() arguments.
212 $items = apply_filters( 'wp_nav_menu_items', $items, $args );
214 * Filters the HTML list content for a specific navigation menu.
220 * @param string $items The HTML list content for the menu items.
221 * @param object $args An object containing wp_nav_menu() arguments.
223 $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
225 // Don't print any markup if there are no items at this point.
226 if ( empty( $items ) )
229 $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
232 if ( $show_container )
233 $nav_menu .= '</' . $args->container . '>';
236 * Filters the HTML content for navigation menus.
242 * @param string $nav_menu The HTML content for the navigation menu.
243 * @param object $args An object containing wp_nav_menu() arguments.
245 $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
254 * Add the class property classes for the current context, if applicable.
259 * @global WP_Query $wp_query
260 * @global WP_Rewrite $wp_rewrite
262 * @param array $menu_items The current menu item objects to which to add the class property information.
264 function _wp_menu_item_classes_by_context( &$menu_items ) {
265 global $wp_query, $wp_rewrite;
267 $queried_object = $wp_query->get_queried_object();
268 $queried_object_id = (int) $wp_query->queried_object_id;
271 $active_ancestor_item_ids = array();
272 $active_parent_item_ids = array();
273 $active_parent_object_ids = array();
274 $possible_taxonomy_ancestors = array();
275 $possible_object_parents = array();
276 $home_page_id = (int) get_option( 'page_for_posts' );
278 if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
279 foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
280 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
281 $term_hierarchy = _get_term_hierarchy( $taxonomy );
282 $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
283 if ( is_array( $terms ) ) {
284 $possible_object_parents = array_merge( $possible_object_parents, $terms );
285 $term_to_ancestor = array();
286 foreach ( (array) $term_hierarchy as $anc => $descs ) {
287 foreach ( (array) $descs as $desc )
288 $term_to_ancestor[ $desc ] = $anc;
291 foreach ( $terms as $desc ) {
293 $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
294 if ( isset( $term_to_ancestor[ $desc ] ) ) {
295 $_desc = $term_to_ancestor[ $desc ];
296 unset( $term_to_ancestor[ $desc ] );
301 } while ( ! empty( $desc ) );
306 } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
307 $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
308 $term_to_ancestor = array();
309 foreach ( (array) $term_hierarchy as $anc => $descs ) {
310 foreach ( (array) $descs as $desc )
311 $term_to_ancestor[ $desc ] = $anc;
313 $desc = $queried_object->term_id;
315 $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
316 if ( isset( $term_to_ancestor[ $desc ] ) ) {
317 $_desc = $term_to_ancestor[ $desc ];
318 unset( $term_to_ancestor[ $desc ] );
323 } while ( ! empty( $desc ) );
326 $possible_object_parents = array_filter( $possible_object_parents );
328 $front_page_url = home_url();
330 foreach ( (array) $menu_items as $key => $menu_item ) {
332 $menu_items[$key]->current = false;
334 $classes = (array) $menu_item->classes;
335 $classes[] = 'menu-item';
336 $classes[] = 'menu-item-type-' . $menu_item->type;
337 $classes[] = 'menu-item-object-' . $menu_item->object;
339 // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
340 if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
341 $active_parent_object_ids[] = (int) $menu_item->object_id;
342 $active_parent_item_ids[] = (int) $menu_item->db_id;
343 $active_object = $queried_object->post_type;
345 // if the menu item corresponds to the currently-queried post or taxonomy object
347 $menu_item->object_id == $queried_object_id &&
349 ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
350 ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
351 ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
354 $classes[] = 'current-menu-item';
355 $menu_items[$key]->current = true;
356 $_anc_id = (int) $menu_item->db_id;
359 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
360 ! in_array( $_anc_id, $active_ancestor_item_ids )
362 $active_ancestor_item_ids[] = $_anc_id;
365 if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
366 // Back compat classes for pages to match wp_page_menu()
367 $classes[] = 'page_item';
368 $classes[] = 'page-item-' . $menu_item->object_id;
369 $classes[] = 'current_page_item';
371 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
372 $active_parent_object_ids[] = (int) $menu_item->post_parent;
373 $active_object = $menu_item->object;
375 // if the menu item corresponds to the currently-queried post type archive
377 'post_type_archive' == $menu_item->type &&
378 is_post_type_archive( array( $menu_item->object ) )
380 $classes[] = 'current-menu-item';
381 $menu_items[$key]->current = true;
382 // if the menu item corresponds to the currently-requested URL
383 } elseif ( 'custom' == $menu_item->object && isset( $_SERVER['HTTP_HOST'] ) ) {
384 $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
385 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
386 $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
387 $item_url = set_url_scheme( untrailingslashit( $raw_item_url ) );
388 $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) );
390 if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) {
391 $classes[] = 'current-menu-item';
392 $menu_items[$key]->current = true;
393 $_anc_id = (int) $menu_item->db_id;
396 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
397 ! in_array( $_anc_id, $active_ancestor_item_ids )
399 $active_ancestor_item_ids[] = $_anc_id;
402 if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
403 // Back compat for home link to match wp_page_menu()
404 $classes[] = 'current_page_item';
406 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
407 $active_parent_object_ids[] = (int) $menu_item->post_parent;
408 $active_object = $menu_item->object;
410 // give front page item current-menu-item class when extra query arguments involved
411 } elseif ( $item_url == $front_page_url && is_front_page() ) {
412 $classes[] = 'current-menu-item';
415 if ( untrailingslashit($item_url) == home_url() )
416 $classes[] = 'menu-item-home';
419 // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
420 if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
421 $classes[] = 'current_page_parent';
423 $menu_items[$key]->classes = array_unique( $classes );
425 $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
426 $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
427 $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
429 // set parent's class
430 foreach ( (array) $menu_items as $key => $parent_item ) {
431 $classes = (array) $parent_item->classes;
432 $menu_items[$key]->current_item_ancestor = false;
433 $menu_items[$key]->current_item_parent = false;
436 isset( $parent_item->type ) &&
438 // ancestral post object
440 'post_type' == $parent_item->type &&
441 ! empty( $queried_object->post_type ) &&
442 is_post_type_hierarchical( $queried_object->post_type ) &&
443 in_array( $parent_item->object_id, $queried_object->ancestors ) &&
444 $parent_item->object != $queried_object->ID
449 'taxonomy' == $parent_item->type &&
450 isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
451 in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
453 ! isset( $queried_object->term_id ) ||
454 $parent_item->object_id != $queried_object->term_id
459 $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
462 if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
463 $classes[] = 'current-menu-ancestor';
464 $menu_items[$key]->current_item_ancestor = true;
466 if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
467 $classes[] = 'current-menu-parent';
468 $menu_items[$key]->current_item_parent = true;
470 if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
471 $classes[] = 'current-' . $active_object . '-parent';
473 if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
474 // Back compat classes for pages to match wp_page_menu()
475 if ( in_array('current-menu-parent', $classes) )
476 $classes[] = 'current_page_parent';
477 if ( in_array('current-menu-ancestor', $classes) )
478 $classes[] = 'current_page_ancestor';
481 $menu_items[$key]->classes = array_unique( $classes );
486 * Retrieve the HTML list content for nav menu items.
488 * @uses Walker_Nav_Menu to create HTML list content.
491 * @param array $items
496 function walk_nav_menu_tree( $items, $depth, $r ) {
497 $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
498 $args = array( $items, $depth, $r );
500 return call_user_func_array( array( $walker, 'walk' ), $args );
504 * Prevents a menu item ID from being used more than once.
509 * @staticvar array $used_ids
511 * @param object $item
514 function _nav_menu_item_id_use_once( $id, $item ) {
515 static $_used_ids = array();
516 if ( in_array( $item->ID, $_used_ids ) ) {
519 $_used_ids[] = $item->ID;