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

Retrieves all menu items of a navigation menu.


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.


Top ↑

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.
More Arguments from get_posts( ... $args ) Array or string of Query parameters.
  • attachment_id int
    Attachment post ID. Used for 'attachment' post_type.
  • author int|string
    Author ID, or comma-separated list of IDs.
  • author_name string
    User 'user_nicename'.
  • author__in int[]
    An array of author IDs to query from.
  • author__not_in int[]
    An array of author IDs not to query from.
  • cache_results bool
    Whether to cache post information. Default true.
  • cat int|string
    Category ID or comma-separated list of IDs (this or any children).
  • category__and int[]
    An array of category IDs (AND in).
  • category__in int[]
    An array of category IDs (OR in, no children).
  • category__not_in int[]
    An array of category IDs (NOT in).
  • category_name string
    Use category slug (not name, this or any children).
  • comment_count array|int
    Filter results by comment count. Provide an integer to match comment count exactly. Provide an array with integer 'value' and 'compare' operator ('=', '!=', '>', '>=', '<', '<=' ) to compare against comment_count in a specific way.
  • comment_status string
    Comment status.
  • comments_per_page int
    The number of comments to return per page.
    Default 'comments_per_page' option.
  • date_query array
    An associative array of WP_Date_Query arguments.
    See WP_Date_Query::__construct().
  • day int
    Day of the month. Accepts numbers 1-31.
  • exact bool
    Whether to search by exact keyword. Default false.
  • fields string
    Post fields to query for. Accepts:
    • '' Returns an array of complete post objects (WP_Post[]).
    • 'ids' Returns an array of post IDs (int[]).
    • 'id=>parent' Returns an associative array of parent post IDs, keyed by post ID (int[]).
    Default ''.
  • hour int
    Hour of the day. Accepts numbers 0-23.
  • ignore_sticky_posts int|bool
    Whether to ignore sticky posts or not. Setting this to false excludes stickies from 'post__in'. Accepts 1|true, 0|false.
    Default false.
  • m int
    Combination YearMonth. Accepts any four-digit year and month numbers 01-12.
  • meta_key string|string[]
    Meta key or keys to filter by.
  • meta_value string|string[]
    Meta value or values to filter by.
  • meta_compare string
    MySQL operator used for comparing the meta value.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_compare_key string
    MySQL operator used for comparing the meta key.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_type string
    MySQL data type that the meta_value column will be CAST to for comparisons.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_type_key string
    MySQL data type that the meta_key column will be CAST to for comparisons.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_query array
    An associative array of WP_Meta_Query arguments.
    See WP_Meta_Query::__construct() for accepted values.
  • menu_order int
    The menu order of the posts.
  • minute int
    Minute of the hour. Accepts numbers 0-59.
  • monthnum int
    The two-digit month. Accepts numbers 1-12.
  • name string
    Post slug.
  • nopaging bool
    Show all posts (true) or paginate (false). Default false.
  • no_found_rows bool
    Whether to skip counting the total rows found. Enabling can improve performance. Default false.
  • offset int
    The number of posts to offset before retrieval.
  • order string
    Designates ascending or descending order of posts. Default 'DESC'.
    Accepts 'ASC', 'DESC'.
  • orderby string|array
    Sort retrieved posts by parameter. One or more options may be passed.
    To use 'meta_value', or 'meta_value_num', 'meta_key=keyname' must be also be defined. To sort by a specific $meta_query clause, use that clause's array key. Accepts:
    • 'none'
    • 'name'
    • 'author'
    • 'date'
    • 'title'
    • 'modified'
    • 'menu_order'
    • 'parent'
    • 'ID'
    • 'rand'
    • 'relevance'
    • 'RAND(x)' (where 'x' is an integer seed value)
    • 'comment_count'
    • 'meta_value'
    • 'meta_value_num'
    • 'post__in'
    • 'post_name__in'
    • 'post_parent__in'
    • The array keys of $meta_query.
    Default is 'date', except when a search is being performed, when the default is 'relevance'.
  • p int
    Post ID.
  • page int
    Show the number of posts that would show up on page X of a static front page.
  • paged int
    The number of the current page.
  • page_id int
    Page ID.
  • pagename string
    Page slug.
  • perm string
    Show posts if user has the appropriate capability.
  • ping_status string
    Ping status.
  • post__in int[]
    An array of post IDs to retrieve, sticky posts will be included.
  • post__not_in int[]
    An array of post IDs not to retrieve. Note: a string of comma- separated IDs will NOT work.
  • post_mime_type string
    The mime type of the post. Used for 'attachment' post_type.
  • post_name__in string[]
    An array of post slugs that results must match.
  • post_parent int
    Page ID to retrieve child pages for. Use 0 to only retrieve top-level pages.
  • post_parent__in int[]
    An array containing parent page IDs to query child pages from.
  • post_parent__not_in int[]
    An array containing parent page IDs not to query child pages from.
  • post_type string|string[]
    A post type slug (string) or array of post type slugs.
    Default 'any' if using 'tax_query'.
  • post_status string|string[]
    A post status (string) or array of post statuses.
  • posts_per_page int
    The number of posts to query for. Use -1 to request all posts.
  • posts_per_archive_page int
    The number of posts to query for by archive page. Overrides 'posts_per_page' when is_archive() , or is_search() are true.
  • s string
    Search keyword(s). Prepending a term with a hyphen will exclude posts matching that term. Eg, 'pillow -sofa' will return posts containing 'pillow' but not 'sofa'. The character used for exclusion can be modified using the the 'wp_query_search_exclusion_prefix' filter.
  • second int
    Second of the minute. Accepts numbers 0-59.
  • sentence bool
    Whether to search by phrase. Default false.
  • suppress_filters bool
    Whether to suppress filters. Default false.
  • tag string
    Tag slug. Comma-separated (either), Plus-separated (all).
  • tag__and int[]
    An array of tag IDs (AND in).
  • tag__in int[]
    An array of tag IDs (OR in).
  • tag__not_in int[]
    An array of tag IDs (NOT in).
  • tag_id int
    Tag id or comma-separated list of IDs.
  • tag_slug__and string[]
    An array of tag slugs (AND in).
  • tag_slug__in string[]
    An array of tag slugs (OR in). unless 'ignore_sticky_posts' is true. Note: a string of comma-separated IDs will NOT work.
  • tax_query array
    An associative array of WP_Tax_Query arguments.
    See WP_Tax_Query::__construct().
  • title string
    Post title.
  • update_post_meta_cache bool
    Whether to update the post meta cache. Default true.
  • update_post_term_cache bool
    Whether to update the post term cache. Default true.
  • update_menu_item_cache bool
    Whether to update the menu item cache. Default false.
  • lazy_load_term_meta bool
    Whether to lazy-load term meta. Setting to false will disable cache priming for term meta, so that each get_term_meta() call will hit the database.
    Defaults to the value of $update_post_term_cache.
  • w int
    The week number of the year. Accepts numbers 0-53.
  • year int
    The four-digit year. Accepts any four-digit year.

Default: array()


Top ↑

Return

array|false Array of menu items, otherwise false.


Top ↑

Source

File: wp-includes/nav-menu.php. View all references

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

	if ( ! $menu ) {
		return false;
	}

	if ( ! taxonomy_exists( 'nav_menu' ) ) {
		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,
		'update_menu_item_cache' => true,
		'tax_query'              => array(
			array(
				'taxonomy' => 'nav_menu',
				'field'    => 'term_taxonomy_id',
				'terms'    => $menu->term_taxonomy_id,
			),
		),
	);
	$args     = wp_parse_args( $args, $defaults );
	if ( $menu->count > 0 ) {
		$items = get_posts( $args );
	} else {
		$items = array();
	}

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

	if ( ! is_admin() ) { // Remove invalid items only on 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 ↑

Hooks



Top ↑

Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

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

    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;
        
    }
  4. Skip to note 4 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
  5. Skip to note 5 content
    Contributed by Aslam Ansari
    /**
     * This code will generate a simple array of menu items in hierarchical(parent-child) order.
     * This array will also contain child field as "true" if the item has child items array, 
     * and custom active class if the link is active.
     */
    $menu_lists = array();
    $sub_parent = 0;
    
    // Menu item array (note: "Menu 1" is the name of menu)
    $menu_items = wp_get_nav_menu_items( 'Menu 1' );
    foreach ( $menu_items as $menu_item ) {
    
        if ( in_array( $menu_item->object, array( 'page', 'custom' ) ) ) {
            $id = $menu_item->ID;
            $title = $menu_item->title;
            $link = $menu_item->url;
            $menu_item_parent = $menu_item->menu_item_parent;
    
            // if menu item has no parent, means this is the top-menu.
            if ( ! $menu_item_parent ) {
                $menu_lists[ $id ]['child'] = false;
                $menu_lists[ $id ]['id'] = $id;
                $menu_lists[ $id ]['title'] = $title;
                $menu_lists[ $id ]['link'] = $link;
    
                // add active field if current link and open url is same.
                if ( get_permalink() === $link ) {
                    $menu_lists[ $id ]['active'] = 'current-menu-item';
                }
            } else {
                // if parent menu is set, means this is 2nd level menu
                if ( isset( $menu_lists[ $menu_item_parent ] ) ) {
                    $menu_lists[ $menu_item_parent ]['child'] = true;
                    $menu_lists[ $menu_item_parent ][ $id ]['child'] = false;
                    $menu_lists[ $menu_item_parent ][ $id ]['id'] = $id;
                    $menu_lists[ $menu_item_parent ][ $id ]['title'] = $title;
                    $menu_lists[ $menu_item_parent ][ $id ]['link'] = $link;
    
                    // add active field to current menu item and its parent menu item if current link and open url is same.
                    if ( get_permalink() === $link ) {
                        $menu_lists[ $menu_item_parent ]['active'] = 'current-menu-item';
                        $menu_lists[ $menu_item_parent ][ $id ]['active'] = 'current-menu-item';
                    }
    
                    $sub_parent = $menu_item_parent;
                } elseif ( isset( $menu_lists[ $sub_parent ][ $menu_item_parent ] ) ) {
                    // if parent menu is set and their parent menu is also set, means this is 3rd level menu
                    $menu_lists[ $sub_parent ][ $menu_item_parent ]['child'] = true;
                    $menu_lists[ $sub_parent ][ $menu_item_parent ][ $id ]['id'] = $id;
                    $menu_lists[ $sub_parent ][ $menu_item_parent ][ $id ]['title'] = $title;
                    $menu_lists[ $sub_parent ][ $menu_item_parent ][ $id ]['link'] = $link;
    
                    // add active field to current menu item and its parent menu item if current link and open url is same.
                    if ( get_permalink() === $link ) {
                        $menu_lists[ $sub_parent ]['active'] = 'current-menu-item';
                        $menu_lists[ $sub_parent ][ $menu_item_parent ]['active'] = 'current-menu-item';
                        $menu_lists[ $sub_parent ][ $menu_item_parent ][ $id ]['active'] = 'current-menu-item';
                    }
                }
            }
        }
    }
  6. Skip to note 6 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>';
    }
    }
  7. Skip to note 7 content
    Contributed by mathiasmadsen

    Here are two functions that can be used to add the menu to the REST API and retrieve individual menu items:

    function wpdocs_menu_route() {
    	$menuLocations = get_nav_menu_locations(); // Get nav locations set in theme, usually functions.php)
    	return $menuLocations;
    }
    
    add_action( 'rest_api_init', function () {
    	register_rest_route( 'custom', '/menu/', array(
    		'methods' => 'GET',
    		'callback' => 'wpdocs_menu_route',
    	) );
    } );
    
    function wpdocs_menu_single( $data ) {
    	$menuID = $data['id']; // Get the menu from the ID
    	$primaryNav = wp_get_nav_menu_items( $menuID ); // Get the array of wp objects, the nav items for our queried location.
    	return $primaryNav;
    }
    
    add_action( 'rest_api_init', function () {
    	register_rest_route( 'custom', '/menu/(?P<id>d+)', array(
    		'methods' => 'GET',
    		'callback' => 'wpdocs_menu_single',
    	) );
    } );

    This allows us to access both:
    https://example.com/wp-json/custom/menu and
    https://example.com/wp-json/custom/menu/{menuID}

  8. Skip to note 8 content
    Contributed by rachelboylson

    Create a shortcode that takes a menu ID and prints a simple list of links. Add this to your functions.php file.
    Shortcode syntax: [my_custom_navigation menu_id=123]

    // load navigation shortcode
    add_shortcode( 'wpdocs_custom_navigation', 'wpdocs_add_custom_navigation' );
    
    // create shortcode for navigation
    function wpdocs_add_custom_navigation( $attributes, $content = null ) {
    
        // shortcode takes in one argument which is the menu id
        $navigation_attributes = shortcode_atts( array(
            'menu_id' => '',
        ), $attributes );
    
        // get navigation menu items based on menu id passed from shortcode
        $menu_items = wp_get_nav_menu_items( $navigation_attributes['menu_id'] );
    
        // create an empty string which we will add onto
        $menu_html = '';
    
        // loop through array and build list of navigation links
        for ( $x = 0; $x < count( $menu_items ); $x++ ) {
    
            // get URL of menu item
            $url = $menu_items[ $x ]->url;
    
            // get title of menu item
            $title = $menu_items[ $x ]->title;
    
            // build HTML
            $menu_html .= '<a href="';
            $menu_html .= esc_attr( esc_url( $url ) );
            $menu_html .= '">';
            $menu_html .= wp_kses_post( $title );
            $menu_html .= '</a><br>';
        }
    
        // return navigation HTML
        return
        '<div id="menuWrapper">'
            . $menu_html .
        '</div>';
    }
  9. Skip to note 10 content
    Contributed by rohit0106

    Build 3 level hierarchical menu

    function create_bootstrap_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);
    		//echo '<pre>'; print_r($menu_items); die;
            $menu_list = '<ul>';
            $menucount = 1;
    		$bool = true;
            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 class="dropdown"><a href="' . $submenu->url . '" >' . $submenu->title . '</a><ul>';
    						$parents = $submenu->ID;
    						foreach($menu_items as $submenus){
    							if( $submenus->menu_item_parent == $parents ) {
    								$menu_array[] .= '<li><a href="' . $submenus->url . '" >' . $submenus->title . '</a></li>';
    							}
    						}
    						$menu_array[] .= '</ul></li>';
                        }
                    }
                    if( $bool == true && count( $menu_array ) > 0 ) {
                         
                        $menu_list .= '<li class="dropdown">';
                        $menu_list .= '<a href="'.$menu_item->url.'"><span>'.$menu_item->title.'</span> <i class="bi bi-chevron-down"></i></a>';
                         
                        $menu_list .= '<ul>' ."\n";
                        $menu_list .= implode( $menu_array );
                        $menu_list .= '</ul>';
                         
                    } else {
                        // echo "<pre>"; print_r($menu_item); 
                        $menu_list .= '<li>';
                        $menu_list .= '<a class="nav-link scrollto active" href="'.$menu_item->url.'">' . $menu_item->title . '</a>';
                    }
                     
                }
                 
                // end <li>
                $menu_list .= '</li>';
    			
    			$menucount++;
            }
        } else {
            $menu_list = '<!-- no menu defined in location "'.$theme_location.'" -->';
        }
         
        return $menu_list;
    }

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