3 * Navigation Menu template functions
6 * @subpackage Nav_Menus
11 * Create HTML list of nav menu items.
17 class Walker_Nav_Menu extends Walker {
19 * @see Walker::$tree_type
23 var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
26 * @see Walker::$db_fields
28 * @todo Decouple this.
31 var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
34 * @see Walker::start_lvl()
37 * @param string $output Passed by reference. Used to append additional content.
38 * @param int $depth Depth of page. Used for padding.
40 function start_lvl( &$output, $depth = 0, $args = array() ) {
41 $indent = str_repeat("\t", $depth);
42 $output .= "\n$indent<ul class=\"sub-menu\">\n";
46 * @see Walker::end_lvl()
49 * @param string $output Passed by reference. Used to append additional content.
50 * @param int $depth Depth of page. Used for padding.
52 function end_lvl( &$output, $depth = 0, $args = array() ) {
53 $indent = str_repeat("\t", $depth);
54 $output .= "$indent</ul>\n";
58 * @see Walker::start_el()
61 * @param string $output Passed by reference. Used to append additional content.
62 * @param object $item Menu item data object.
63 * @param int $depth Depth of menu item. Used for padding.
64 * @param int $current_page Menu item ID.
67 function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
68 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
70 $class_names = $value = '';
72 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
73 $classes[] = 'menu-item-' . $item->ID;
75 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
76 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
78 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
79 $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
81 $output .= $indent . '<li' . $id . $value . $class_names .'>';
83 $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
84 $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
85 $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
86 $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
88 $item_output = $args->before;
89 $item_output .= '<a'. $attributes .'>';
90 $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
91 $item_output .= '</a>';
92 $item_output .= $args->after;
94 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
98 * @see Walker::end_el()
101 * @param string $output Passed by reference. Used to append additional content.
102 * @param object $item Page data object. Not used.
103 * @param int $depth Depth of page. Not Used.
105 function end_el( &$output, $item, $depth = 0, $args = array() ) {
106 $output .= "</li>\n";
111 * Displays a navigation menu.
113 * Optional $args contents:
115 * menu - The menu that is desired. Accepts (matching in order) id, slug, name. Defaults to blank.
116 * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'.
117 * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented.
118 * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'.
119 * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'.
120 * container_id - The ID that is applied to the container. Defaults to blank.
121 * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'. Set to false for no fallback.
122 * before - Text before the link text.
123 * after - Text after the link text.
124 * link_before - Text before the link.
125 * link_after - Text after the link.
126 * echo - Whether to echo the menu or return it. Defaults to echo.
127 * depth - how many levels of the hierarchy are to be included. 0 means all. Defaults to 0.
128 * walker - allows a custom walker to be specified.
129 * 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.
130 * items_wrap - How the list items should be wrapped. Defaults to a ul with an id and class. Uses printf() format with numbered placeholders.
134 * @param array $args Arguments
136 function wp_nav_menu( $args = array() ) {
137 static $menu_id_slugs = array();
139 $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
140 '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>',
141 'depth' => 0, 'walker' => '', 'theme_location' => '' );
143 $args = wp_parse_args( $args, $defaults );
144 $args = apply_filters( 'wp_nav_menu_args', $args );
145 $args = (object) $args;
147 // Get the nav menu based on the requested menu
148 $menu = wp_get_nav_menu_object( $args->menu );
150 // Get the nav menu based on the theme_location
151 if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
152 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
154 // get the first menu that has items if we still can't find a menu
155 if ( ! $menu && !$args->theme_location ) {
156 $menus = wp_get_nav_menus();
157 foreach ( $menus as $menu_maybe ) {
158 if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) {
165 // If the menu exists, get its items.
166 if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
167 $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
170 * If no menu was found:
171 * - Fallback (if one was specified), or bail.
173 * If no menu items were found:
174 * - Fallback, but only if no theme location was specified.
177 if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
178 && $args->fallback_cb && is_callable( $args->fallback_cb ) )
179 return call_user_func( $args->fallback_cb, (array) $args );
181 if ( !$menu || is_wp_error( $menu ) || empty( $menu_items ) )
184 $nav_menu = $items = '';
186 $show_container = false;
187 if ( $args->container ) {
188 $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
189 if ( in_array( $args->container, $allowed_tags ) ) {
190 $show_container = true;
191 $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
192 $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
193 $nav_menu .= '<'. $args->container . $id . $class . '>';
197 // Set up the $menu_item variables
198 _wp_menu_item_classes_by_context( $menu_items );
200 $sorted_menu_items = array();
201 foreach ( (array) $menu_items as $key => $menu_item )
202 $sorted_menu_items[$menu_item->menu_order] = $menu_item;
206 $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
208 $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
209 unset($sorted_menu_items);
212 if ( ! empty( $args->menu_id ) ) {
213 $wrap_id = $args->menu_id;
215 $wrap_id = 'menu-' . $menu->slug;
216 while ( in_array( $wrap_id, $menu_id_slugs ) ) {
217 if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
218 $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
220 $wrap_id = $wrap_id . '-1';
223 $menu_id_slugs[] = $wrap_id;
225 $wrap_class = $args->menu_class ? $args->menu_class : '';
227 // Allow plugins to hook into the menu to add their own <li>'s
228 $items = apply_filters( 'wp_nav_menu_items', $items, $args );
229 $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
231 $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
234 if ( $show_container )
235 $nav_menu .= '</' . $args->container . '>';
237 $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
246 * Add the class property classes for the current context, if applicable.
251 * @param array $menu_items The current menu item objects to which to add the class property information.
253 function _wp_menu_item_classes_by_context( &$menu_items ) {
256 $queried_object = $wp_query->get_queried_object();
257 $queried_object_id = (int) $wp_query->queried_object_id;
260 $active_ancestor_item_ids = array();
261 $active_parent_item_ids = array();
262 $active_parent_object_ids = array();
263 $possible_taxonomy_ancestors = array();
264 $possible_object_parents = array();
265 $home_page_id = (int) get_option( 'page_for_posts' );
267 if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
268 foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
269 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
270 $term_hierarchy = _get_term_hierarchy( $taxonomy );
271 $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
272 if ( is_array( $terms ) ) {
273 $possible_object_parents = array_merge( $possible_object_parents, $terms );
274 $term_to_ancestor = array();
275 foreach ( (array) $term_hierarchy as $anc => $descs ) {
276 foreach ( (array) $descs as $desc )
277 $term_to_ancestor[ $desc ] = $anc;
280 foreach ( $terms as $desc ) {
282 $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
283 if ( isset( $term_to_ancestor[ $desc ] ) ) {
284 $_desc = $term_to_ancestor[ $desc ];
285 unset( $term_to_ancestor[ $desc ] );
290 } while ( ! empty( $desc ) );
295 } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
296 $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
297 $term_to_ancestor = array();
298 foreach ( (array) $term_hierarchy as $anc => $descs ) {
299 foreach ( (array) $descs as $desc )
300 $term_to_ancestor[ $desc ] = $anc;
302 $desc = $queried_object->term_id;
304 $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
305 if ( isset( $term_to_ancestor[ $desc ] ) ) {
306 $_desc = $term_to_ancestor[ $desc ];
307 unset( $term_to_ancestor[ $desc ] );
312 } while ( ! empty( $desc ) );
315 $possible_object_parents = array_filter( $possible_object_parents );
317 $front_page_url = home_url();
319 foreach ( (array) $menu_items as $key => $menu_item ) {
321 $menu_items[$key]->current = false;
323 $classes = (array) $menu_item->classes;
324 $classes[] = 'menu-item';
325 $classes[] = 'menu-item-type-' . $menu_item->type;
326 $classes[] = 'menu-item-object-' . $menu_item->object;
328 // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
329 if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
330 $active_parent_object_ids[] = (int) $menu_item->object_id;
331 $active_parent_item_ids[] = (int) $menu_item->db_id;
332 $active_object = $queried_object->post_type;
334 // if the menu item corresponds to the currently-queried post or taxonomy object
336 $menu_item->object_id == $queried_object_id &&
338 ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
339 ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
340 ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
343 $classes[] = 'current-menu-item';
344 $menu_items[$key]->current = true;
345 $_anc_id = (int) $menu_item->db_id;
348 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
349 ! in_array( $_anc_id, $active_ancestor_item_ids )
351 $active_ancestor_item_ids[] = $_anc_id;
354 if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
355 // Back compat classes for pages to match wp_page_menu()
356 $classes[] = 'page_item';
357 $classes[] = 'page-item-' . $menu_item->object_id;
358 $classes[] = 'current_page_item';
360 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
361 $active_parent_object_ids[] = (int) $menu_item->post_parent;
362 $active_object = $menu_item->object;
364 // if the menu item corresponds to the currently-requested URL
365 } elseif ( 'custom' == $menu_item->object ) {
366 $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
367 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
368 $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
369 $item_url = untrailingslashit( $raw_item_url );
370 $_indexless_current = untrailingslashit( preg_replace( '/index.php$/', '', $current_url ) );
372 if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) {
373 $classes[] = 'current-menu-item';
374 $menu_items[$key]->current = true;
375 $_anc_id = (int) $menu_item->db_id;
378 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
379 ! in_array( $_anc_id, $active_ancestor_item_ids )
381 $active_ancestor_item_ids[] = $_anc_id;
384 if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
385 // Back compat for home link to match wp_page_menu()
386 $classes[] = 'current_page_item';
388 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
389 $active_parent_object_ids[] = (int) $menu_item->post_parent;
390 $active_object = $menu_item->object;
392 // give front page item current-menu-item class when extra query arguments involved
393 } elseif ( $item_url == $front_page_url && is_front_page() ) {
394 $classes[] = 'current-menu-item';
397 if ( untrailingslashit($item_url) == home_url() )
398 $classes[] = 'menu-item-home';
401 // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
402 if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
403 $classes[] = 'current_page_parent';
405 $menu_items[$key]->classes = array_unique( $classes );
407 $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
408 $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
409 $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
411 // set parent's class
412 foreach ( (array) $menu_items as $key => $parent_item ) {
413 $classes = (array) $parent_item->classes;
414 $menu_items[$key]->current_item_ancestor = false;
415 $menu_items[$key]->current_item_parent = false;
418 isset( $parent_item->type ) &&
420 // ancestral post object
422 'post_type' == $parent_item->type &&
423 ! empty( $queried_object->post_type ) &&
424 is_post_type_hierarchical( $queried_object->post_type ) &&
425 in_array( $parent_item->object_id, $queried_object->ancestors ) &&
426 $parent_item->object != $queried_object->ID
431 'taxonomy' == $parent_item->type &&
432 isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
433 in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
435 ! isset( $queried_object->term_id ) ||
436 $parent_item->object_id != $queried_object->term_id
441 $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
444 if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
445 $classes[] = 'current-menu-ancestor';
446 $menu_items[$key]->current_item_ancestor = true;
448 if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
449 $classes[] = 'current-menu-parent';
450 $menu_items[$key]->current_item_parent = true;
452 if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
453 $classes[] = 'current-' . $active_object . '-parent';
455 if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
456 // Back compat classes for pages to match wp_page_menu()
457 if ( in_array('current-menu-parent', $classes) )
458 $classes[] = 'current_page_parent';
459 if ( in_array('current-menu-ancestor', $classes) )
460 $classes[] = 'current_page_ancestor';
463 $menu_items[$key]->classes = array_unique( $classes );
468 * Retrieve the HTML list content for nav menu items.
470 * @uses Walker_Nav_Menu to create HTML list content.
472 * @see Walker::walk() for parameters and return description.
474 function walk_nav_menu_tree( $items, $depth, $r ) {
475 $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
476 $args = array( $items, $depth, $r );
478 return call_user_func_array( array($walker, 'walk'), $args );
482 * Prevents a menu item ID from being used more than once.
487 function _nav_menu_item_id_use_once( $id, $item ) {
488 static $_used_ids = array();
489 if ( in_array( $item->ID, $_used_ids ) )
491 $_used_ids[] = $item->ID;
494 add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );