wp_get_nav_menu_items( int|string|WP_Term $menu, array $args = array() )

Retrieves all menu items of a navigation menu.


Description Description

Note: Most arguments passed to the $args parameter – save for ‘output_key’ – are specifically for retrieving nav_menu_item posts from get_posts() and may only indirectly affect the ultimate ordering and content of the resulting nav menu items that get returned from this function.


Parameters Parameters

$menu

(int|string|WP_Term) (Required) Menu ID, slug, name, or object.

$args

(array) (Optional) Arguments to pass to get_posts().

  • 'order'
    (string) How to order nav menu items as queried with get_posts(). Will be ignored if 'output' is ARRAY_A. Default 'ASC'.
  • 'orderby'
    (string) Field to order menu items by as retrieved from get_posts(). Supply an orderby field via 'output_key' to affect the output order of nav menu items. Default 'menu_order'.
  • 'post_type'
    (string) Menu items post type. Default 'nav_menu_item'.
  • 'post_status'
    (string) Menu items post status. Default 'publish'.
  • 'output'
    (string) How to order outputted menu items. Default ARRAY_A.
  • 'output_key'
    (string) Key to use for ordering the actual menu items that get returned. Note that that is not a get_posts() argument and will only affect output of menu items processed in this function. Default 'menu_order'.
  • 'nopaging'
    (bool) Whether to retrieve all menu items (true) or paginate (false). Default true.

Default value: array()


Top ↑

Return Return

(false|array) $items Array of menu items, otherwise false.


Top ↑

Source Source

File: wp-includes/nav-menu.php

function wp_get_nav_menu_items( $menu, $args = array() ) {
	$menu = wp_get_nav_menu_object( $menu );

	if ( ! $menu ) {
		return false;
	}

	static $fetched = array();

	$items = get_objects_in_term( $menu->term_id, 'nav_menu' );
	if ( is_wp_error( $items ) ) {
		return false;
	}

	$defaults        = array(
		'order'       => 'ASC',
		'orderby'     => 'menu_order',
		'post_type'   => 'nav_menu_item',
		'post_status' => 'publish',
		'output'      => ARRAY_A,
		'output_key'  => 'menu_order',
		'nopaging'    => true,
	);
	$args            = wp_parse_args( $args, $defaults );
	$args['include'] = $items;

	if ( ! empty( $items ) ) {
		$items = get_posts( $args );
	} else {
		$items = array();
	}

	// Get all posts and terms at once to prime the caches
	if ( empty( $fetched[ $menu->term_id ] ) && ! wp_using_ext_object_cache() ) {
		$fetched[ $menu->term_id ] = true;
		$posts                     = array();
		$terms                     = array();
		foreach ( $items as $item ) {
			$object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
			$object    = get_post_meta( $item->ID, '_menu_item_object', true );
			$type      = get_post_meta( $item->ID, '_menu_item_type', true );

			if ( 'post_type' == $type ) {
				$posts[ $object ][] = $object_id;
			} elseif ( 'taxonomy' == $type ) {
				$terms[ $object ][] = $object_id;
			}
		}

		if ( ! empty( $posts ) ) {
			foreach ( array_keys( $posts ) as $post_type ) {
				get_posts(
					array(
						'post__in'               => $posts[ $post_type ],
						'post_type'              => $post_type,
						'nopaging'               => true,
						'update_post_term_cache' => false,
					)
				);
			}
		}
		unset( $posts );

		if ( ! empty( $terms ) ) {
			foreach ( array_keys( $terms ) as $taxonomy ) {
				get_terms(
					array(
						'taxonomy'     => $taxonomy,
						'include'      => $terms[ $taxonomy ],
						'hierarchical' => false,
					)
				);
			}
		}
		unset( $terms );
	}

	$items = array_map( 'wp_setup_nav_menu_item', $items );

	if ( ! is_admin() ) { // Remove invalid items only in front end
		$items = array_filter( $items, '_is_valid_nav_menu_item' );
	}

	if ( ARRAY_A == $args['output'] ) {
		$items = wp_list_sort(
			$items,
			array(
				$args['output_key'] => 'ASC',
			)
		);
		$i     = 1;
		foreach ( $items as $k => $item ) {
			$items[ $k ]->{$args['output_key']} = $i++;
		}
	}

	/**
	 * Filters the navigation menu items being returned.
	 *
	 * @since 3.0.0
	 *
	 * @param array  $items An array of menu item post objects.
	 * @param object $menu  The menu object.
	 * @param array  $args  An array of arguments used to retrieve menu item objects.
	 */
	return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Stefano

    Building bootstrap 3 menu with submenu items without use WP_nav_walker (boostrap)! (Require bootstrap.css and bootstrap.js)

    <?php
    // Intented to use bootstrap 3.
    // Location is like a 'primary'
    // After, you print menu just add create_bootstrap_menu("primary") in your preferred position;
     
    #add this function in your theme functions.php
     
    function create_bootstrap_menu( $theme_location ) {
        if ( ($theme_location) && ($locations = get_nav_menu_locations()) && isset($locations[$theme_location]) ) {
    		
    		$menu_list  = '<nav class="navbar navbar-default">' ."\n";
            $menu_list .= '<div class="container-fluid">' ."\n";
            $menu_list .= '<!-- Brand and toggle get grouped for better mobile display -->' ."\n";
            $menu_list .= '<div class="navbar-header">' ."\n";
            $menu_list .= '<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">' ."\n";
            $menu_list .= '<span class="sr-only">Toggle navigation</span>' ."\n";
            $menu_list .= '<span class="icon-bar"></span>' ."\n";
            $menu_list .= '<span class="icon-bar"></span>' ."\n";
            $menu_list .= '<span class="icon-bar"></span>' ."\n";
            $menu_list .= '</button>' ."\n";
    		$menu_list .= '<a class="navbar-brand" href="' . home_url() . '">' . get_bloginfo( 'name' ) . '</a>';
            $menu_list .= '</div>' ."\n";
              
            $menu_list .= '<!-- Collect the nav links, forms, and other content for toggling -->';
    		
    		
            $menu = get_term( $locations[$theme_location], 'nav_menu' );
            $menu_items = wp_get_nav_menu_items($menu->term_id);
    
            $menu_list .= '<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">' ."\n";
            $menu_list .= '<ul class="nav navbar-nav">' ."\n";
             
            foreach( $menu_items as $menu_item ) {
    			if( $menu_item->menu_item_parent == 0 ) {
    				
    				$parent = $menu_item->ID;
    				
    				$menu_array = array();
    				foreach( $menu_items as $submenu ) {
    					if( $submenu->menu_item_parent == $parent ) {
    						$bool = true;
    						$menu_array[] = '<li><a href="' . $submenu->url . '">' . $submenu->title . '</a></li>' ."\n";
    					}
    				}
    				if( $bool == true && count( $menu_array ) > 0 ) {
    					
    					$menu_list .= '<li class="dropdown">' ."\n";
    					$menu_list .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $menu_item->title . ' <span class="caret"></span></a>' ."\n";
    					
    					$menu_list .= '<ul class="dropdown-menu">' ."\n";
    					$menu_list .= implode( "\n", $menu_array );
    					$menu_list .= '</ul>' ."\n";
    					
    				} else {
    					
    					$menu_list .= '<li>' ."\n";
    					$menu_list .= '<a href="' . $menu_item->url . '">' . $menu_item->title . '</a>' ."\n";
    				}
    				
    			}
    			
    			// end <li>
    			$menu_list .= '</li>' ."\n";
            }
             
            $menu_list .= '</ul>' ."\n";
            $menu_list .= '</div>' ."\n";
    		$menu_list .= '</div><!-- /.container-fluid -->' ."\n";
            $menu_list .= '</nav>' ."\n";
     
        } else {
            $menu_list = '<!-- no menu defined in location "'.$theme_location.'" -->';
        }
    	
    	echo $menu_list;
    }
    ?>
    
  2. Skip to note 2 content
    Contributed by Andrija Naglic

    Building menu list with children (submenus) and selecting the menu by it’s location:

    // Intented to use with locations, like 'primary'
    // clean_custom_menu("primary");
    
    #add in your theme functions.php file
    
    function clean_custom_menu( $theme_location ) {
    	if ( ($theme_location) && ($locations = get_nav_menu_locations()) && isset($locations[$theme_location]) ) {
    		$menu = get_term( $locations[$theme_location], 'nav_menu' );
    		$menu_items = wp_get_nav_menu_items($menu->term_id);
    
    		$menu_list  = '<nav>' ."\n";
    		$menu_list .= '<ul class="main-nav">' ."\n";
    
    		$count = 0;
    		$submenu = false;
    		
    		foreach( $menu_items as $menu_item ) {
    			
    			$link = $menu_item->url;
    			$title = $menu_item->title;
    			
    			if ( !$menu_item->menu_item_parent ) {
    				$parent_id = $menu_item->ID;
    				
    				$menu_list .= '<li class="item">' ."\n";
    				$menu_list .= '<a href="'.$link.'" class="title">'.$title.'</a>' ."\n";
    			}
    
    			if ( $parent_id == $menu_item->menu_item_parent ) {
    
    				if ( !$submenu ) {
    					$submenu = true;
    					$menu_list .= '<ul class="sub-menu">' ."\n";
    				}
    
    				$menu_list .= '<li class="item">' ."\n";
    				$menu_list .= '<a href="'.$link.'" class="title">'.$title.'</a>' ."\n";
    				$menu_list .= '</li>' ."\n";
    					
    
    				if ( $menu_items[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ){
    					$menu_list .= '</ul>' ."\n";
    					$submenu = false;
    				}
    
    			}
    
    			if ( $menu_items[ $count + 1 ]->menu_item_parent != $parent_id ) { 
    				$menu_list .= '</li>' ."\n";      
    				$submenu = false;
    			}
    
    			$count++;
    		}
    		
    		$menu_list .= '</ul>' ."\n";
    		$menu_list .= '</nav>' ."\n";
    
    	} else {
    		$menu_list = '<!-- no menu defined in location "'.$theme_location.'" -->';
    	}
    	echo $menu_list;
    }
    
  3. Skip to note 3 content
    Contributed by Codex

    Building simple menu list

    // Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
    // This code based on wp_nav_menu's code to get Menu ID from menu slug
    
    $menu_name = 'custom_menu_slug';
    
    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    	$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    
    	$menu_items = wp_get_nav_menu_items($menu->term_id);
    
    	$menu_list = '<ul id="menu-' . $menu_name . '">';
    
    	foreach ( (array) $menu_items as $key => $menu_item ) {
    		$title = $menu_item->title;
    		$url = $menu_item->url;
    		$menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
    	}
    	$menu_list .= '</ul>';
    } else {
    	$menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
    }
    // $menu_list now ready to output
    
    
  4. Skip to note 4 content

    Get simple array of menu.

    function wp_get_menu_array($current_menu) {
    
        $array_menu = wp_get_nav_menu_items($current_menu);
    	$menu = array();
    	foreach ($array_menu as $m) {
    		if (empty($m->menu_item_parent)) {
    			$menu[$m->ID] = array();
    			$menu[$m->ID]['ID'] 		= 	$m->ID;
    			$menu[$m->ID]['title'] 		= 	$m->title;
    			$menu[$m->ID]['url'] 		= 	$m->url;
    			$menu[$m->ID]['children']	= 	array();
    		}
    	}
    	$submenu = array();
    	foreach ($array_menu as $m) {
    		if ($m->menu_item_parent) {
    			$submenu[$m->ID] = array();
    			$submenu[$m->ID]['ID'] 		= 	$m->ID;
    			$submenu[$m->ID]['title']	= 	$m->title;
    			$submenu[$m->ID]['url'] 	= 	$m->url;
    			$menu[$m->menu_item_parent]['children'][$m->ID] = $submenu[$m->ID];
    		}
    	}
        return $menu;
        
    }
    
  5. Skip to note 5 content
    Contributed by Saad Amin

    I have tested this menu , it supports sub menu , submenu will have arrow , also current page parent and menu item will have active class.

    $menu_name = 'header-menu';
    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    	$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
     
    	$menu_items = wp_get_nav_menu_items($menu->term_id);
    	$menu_list = '';
    	$count = 0;
    	$submenu = false;$cpi=get_the_id();
    	foreach( $menu_items as $current ) {
    		if($cpi == $current->object_id ){if ( !$current->menu_item_parent ) {$cpi=$current->ID;}else{$cpi=$current->menu_item_parent;}$cai=$current->ID;break;}
    	}
    	foreach( $menu_items as $menu_item ) {
    		$link = $menu_item->url;
    		$title = $menu_item->title;
    		$menu_item->ID==$cai ? $ac2=' current_menu' : $ac2='';
    		if ( !$menu_item->menu_item_parent ) {
    			$parent_id = $menu_item->ID;$parent_id==$cpi ? $ac=' current_item' : $ac='';
    			if(!empty($menu_items[$count + 1]) && $menu_items[ $count + 1 ]->menu_item_parent == $parent_id ){//Checking has child
    				$menu_list .= '<li class="dropdown has_child'.$ac.'"><a href="'.$link.'" class="dropdown-toggle'.$ac2.'" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="nav-span"></span>'.$title.'<span class="caret"></span></a>';
    			}else{
    				$menu_list .= '<li class="'.$ac.'">' ."\n";$menu_list .= '<a href="'.$link.'" class="'.$ac2.'">'.$title.'</a>' ."\n";
    			}
    			
    		}
    		if ( $parent_id == $menu_item->menu_item_parent ) {
    			if ( !$submenu ) {
    				$submenu = true;
    				$menu_list .= '<ul class="dropdown-menu">' ."\n";
    			}
    			$menu_list .= '<li class="item">' ."\n";
    			$menu_list .= '<a href="'.$link.'" class="'.$ac2.'">'.$title.'</a>' ."\n";
    			$menu_list .= '</li>' ."\n";
    			if(empty($menu_items[$count + 1]) || $menu_items[ $count + 1 ]->menu_item_parent != $parent_id && $submenu){
    				$menu_list .= '</ul>' ."\n";
    				$submenu = false;
    			}
    		}
    		if (empty($menu_items[$count + 1]) || $menu_items[ $count + 1 ]->menu_item_parent != $parent_id ) { 
    			$menu_list .= '</li>' ."\n";      
    			$submenu = false;
    		}
    		$count++;
    	}
    } else {
    	$menu_list = '<li>Menu "' . $menu_name . '" not defined.</li>';
    }
    }
    

You must log in before being able to contribute a note or feedback.