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) {
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) {
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, $args) {
69 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
71 $class_names = $value = '';
73 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
75 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
76 $class_names = ' class="' . esc_attr( $class_names ) . '"';
78 $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
80 $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
81 $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
82 $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
83 $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
85 $item_output = $args->before;
86 $item_output .= '<a'. $attributes .'>';
87 $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
88 $item_output .= '</a>';
89 $item_output .= $args->after;
91 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
95 * @see Walker::end_el()
98 * @param string $output Passed by reference. Used to append additional content.
99 * @param object $item Page data object. Not used.
100 * @param int $depth Depth of page. Not Used.
102 function end_el(&$output, $item, $depth) {
103 $output .= "</li>\n";
108 * Displays a navigation menu.
110 * Optional $args contents:
112 * menu - The menu that is desired. Accepts (matching in order) id, slug, name. Defaults to blank.
113 * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'.
114 * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented.
115 * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'.
116 * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'.
117 * container_id - The ID that is applied to the container. Defaults to blank.
118 * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'.
119 * before - Text before the link text.
120 * after - Text after the link text.
121 * link_before - Text before the link.
122 * link_after - Text after the link.
123 * echo - Whether to echo the menu or return it. Defaults to echo.
124 * depth - how many levels of the hierarchy are to be included. 0 means all. Defaults to 0.
125 * walker - allows a custom walker to be specified.
126 * 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 * @param array $args Arguments
132 function wp_nav_menu( $args = array() ) {
133 static $menu_id_slugs = array();
135 $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
136 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '',
137 'depth' => 0, 'walker' => '', 'theme_location' => '' );
139 $args = wp_parse_args( $args, $defaults );
140 $args = apply_filters( 'wp_nav_menu_args', $args );
141 $args = (object) $args;
143 // Get the nav menu based on the requested menu
144 $menu = wp_get_nav_menu_object( $args->menu );
146 // Get the nav menu based on the theme_location
147 if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
148 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
150 // get the first menu that has items if we still can't find a menu
151 if ( ! $menu && !$args->theme_location ) {
152 $menus = wp_get_nav_menus();
153 foreach ( $menus as $menu_maybe ) {
154 if ( $menu_items = wp_get_nav_menu_items($menu_maybe->term_id) ) {
161 // If the menu exists, get its items.
162 if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
163 $menu_items = wp_get_nav_menu_items( $menu->term_id );
165 // If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists
166 if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
167 && ( function_exists($args->fallback_cb) || is_callable( $args->fallback_cb ) ) )
168 return call_user_func( $args->fallback_cb, (array) $args );
170 // If no fallback function was specified and the menu doesn't exists, bail.
171 if ( !$menu || is_wp_error($menu) )
174 $nav_menu = $items = '';
176 $show_container = false;
177 if ( $args->container ) {
178 $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
179 if ( in_array( $args->container, $allowed_tags ) ) {
180 $show_container = true;
181 $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
182 $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
183 $nav_menu .= '<'. $args->container . $id . $class . '>';
187 // Set up the $menu_item variables
188 _wp_menu_item_classes_by_context( $menu_items );
190 $sorted_menu_items = array();
191 foreach ( (array) $menu_items as $key => $menu_item )
192 $sorted_menu_items[$menu_item->menu_order] = $menu_item;
196 $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
197 unset($sorted_menu_items);
200 if ( ! empty( $args->menu_id ) ) {
201 $slug = $args->menu_id;
203 $slug = 'menu-' . $menu->slug;
204 while ( in_array( $slug, $menu_id_slugs ) ) {
205 if ( preg_match( '#-(\d+)$#', $slug, $matches ) )
206 $slug = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $slug);
208 $slug = $slug . '-1';
211 $menu_id_slugs[] = $slug;
212 $attributes = ' id="' . $slug . '"';
213 $attributes .= $args->menu_class ? ' class="'. $args->menu_class .'"' : '';
215 $nav_menu .= '<ul'. $attributes .'>';
217 // Allow plugins to hook into the menu to add their own <li>'s
218 $items = apply_filters( 'wp_nav_menu_items', $items, $args );
219 $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
223 $nav_menu .= '</ul>';
225 if ( $show_container )
226 $nav_menu .= '</' . $args->container . '>';
228 $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
237 * Add the class property classes for the current context, if applicable.
242 * @param array $menu_items The current menu item objects to which to add the class property information.
244 function _wp_menu_item_classes_by_context( &$menu_items ) {
247 $queried_object = $wp_query->get_queried_object();
248 $queried_object_id = (int) $wp_query->queried_object_id;
251 $active_ancestor_item_ids = array();
252 $active_parent_item_ids = array();
253 $active_parent_object_ids = array();
254 $possible_taxonomy_ancestors = array();
255 $possible_object_parents = array();
256 $home_page_id = (int) get_option( 'page_for_posts' );
258 if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
259 foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
260 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
261 $term_hierarchy = _get_term_hierarchy( $taxonomy );
262 $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
263 if ( is_array( $terms ) ) {
264 $possible_object_parents = array_merge( $possible_object_parents, $terms );
265 $term_to_ancestor = array();
266 foreach ( (array) $term_hierarchy as $anc => $descs ) {
267 foreach ( (array) $descs as $desc )
268 $term_to_ancestor[ $desc ] = $anc;
271 foreach ( $terms as $desc ) {
273 $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
274 if ( isset( $term_to_ancestor[ $desc ] ) ) {
275 $_desc = $term_to_ancestor[ $desc ];
276 unset( $term_to_ancestor[ $desc ] );
281 } while ( ! empty( $desc ) );
286 } elseif ( ! empty( $queried_object->post_type ) && is_post_type_hierarchical( $queried_object->post_type ) ) {
287 _get_post_ancestors( $queried_object );
288 } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
289 $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
290 $term_to_ancestor = array();
291 foreach ( (array) $term_hierarchy as $anc => $descs ) {
292 foreach ( (array) $descs as $desc )
293 $term_to_ancestor[ $desc ] = $anc;
295 $desc = $queried_object->term_id;
297 $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
298 if ( isset( $term_to_ancestor[ $desc ] ) ) {
299 $_desc = $term_to_ancestor[ $desc ];
300 unset( $term_to_ancestor[ $desc ] );
305 } while ( ! empty( $desc ) );
308 $possible_object_parents = array_filter( $possible_object_parents );
310 foreach ( (array) $menu_items as $key => $menu_item ) {
311 $classes = (array) $menu_item->classes;
312 $classes[] = 'menu-item';
313 $classes[] = 'menu-item-type-' . $menu_item->type;
315 // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
316 if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
317 $active_parent_object_ids[] = (int) $menu_item->object_id;
318 $active_parent_item_ids[] = (int) $menu_item->db_id;
319 $active_object = $queried_object->post_type;
321 // if the menu item corresponds to the currently-queried post or taxonomy object
323 $menu_item->object_id == $queried_object_id &&
325 ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
326 ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
327 ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) )
330 $classes[] = 'current-menu-item';
331 $_anc_id = (int) $menu_item->db_id;
334 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
335 ! in_array( $_anc_id, $active_ancestor_item_ids )
337 $active_ancestor_item_ids[] = $_anc_id;
340 if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
341 // Back compat classes for pages to match wp_page_menu()
342 $classes[] = 'page_item';
343 $classes[] = 'page-item-' . $menu_item->object_id;
344 $classes[] = 'current_page_item';
346 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
347 $active_parent_object_ids[] = (int) $menu_item->post_parent;
348 $active_object = $menu_item->object;
350 // if the menu item corresponds to the currently-requested URL
351 } elseif ( 'custom' == $menu_item->object ) {
352 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
353 $item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
354 if ( $item_url == $current_url ) {
355 $classes[] = 'current-menu-item';
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 ( untrailingslashit($current_url) == home_url() ) {
366 $classes[] = 'menu-item-home';
367 // Back compat for home limk to match wp_page_menu()
368 $classes[] = 'current_page_item';
370 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
371 $active_parent_object_ids[] = (int) $menu_item->post_parent;
372 $active_object = $menu_item->object;
376 // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
377 if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
378 $classes[] = 'current_page_parent';
380 $menu_items[$key]->classes = array_unique( $classes );
382 $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
383 $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
384 $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
386 // set parent's class
387 foreach ( (array) $menu_items as $key => $parent_item ) {
388 $classes = (array) $parent_item->classes;
391 isset( $parent_item->type ) &&
393 // ancestral post object
395 'post_type' == $parent_item->type &&
396 ! empty( $queried_object->post_type ) &&
397 is_post_type_hierarchical( $queried_object->post_type ) &&
398 in_array( $parent_item->object_id, $queried_object->ancestors )
403 'taxonomy' == $parent_item->type &&
404 isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
405 in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] )
409 $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
412 if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
413 $classes[] = 'current-menu-ancestor';
415 if ( in_array( $parent_item->db_id, $active_parent_item_ids ) )
416 $classes[] = 'current-menu-parent';
417 if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
418 $classes[] = 'current-' . $active_object . '-parent';
420 if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
421 // Back compat classes for pages to match wp_page_menu()
422 if ( in_array('current-menu-parent', $classes) )
423 $classes[] = 'current_page_parent';
424 if ( in_array('current-menu-ancestor', $classes) )
425 $classes[] = 'current_page_ancestor';
428 $menu_items[$key]->classes = array_unique( $classes );
433 * Retrieve the HTML list content for nav menu items.
435 * @uses Walker_Nav_Menu to create HTML list content.
437 * @see Walker::walk() for parameters and return description.
439 function walk_nav_menu_tree( $items, $depth, $r ) {
440 $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
441 $args = array( $items, $depth, $r );
443 return call_user_func_array( array(&$walker, 'walk'), $args );