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.
Parameters
$menuint|string|WP_Termrequired- Menu ID, slug, name, or object.
$argsarrayoptional- Arguments to pass to get_posts() .
orderstringHow to order nav menu items as queried with get_posts() .
Will be ignored if'output'is ARRAY_A. Default'ASC'.orderbystringField 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_typestringMenu items post type. Default'nav_menu_item'.post_statusstringMenu items post status. Default'publish'.outputstringHow to order outputted menu items. Default ARRAY_A.output_keystringKey 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'.nopagingboolWhether to retrieve all menu items (true) or paginate (false). Default true.update_menu_item_cacheboolWhether to update the menu item cache. Default true.
More Arguments from get_posts( … $args )
Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments.
numberpostsintTotal number of posts to retrieve. Is an alias of$posts_per_pagein WP_Query. Accepts -1 for all. Default 5.categoryint|stringCategory ID or comma-separated list of IDs (this or any children).
Is an alias of$catin WP_Query. Default 0.includeint[]An array of post IDs to retrieve, sticky posts will be included.
Is an alias of$post__inin WP_Query. Default empty array.excludeint[]An array of post IDs not to retrieve. Default empty array.suppress_filtersboolWhether to suppress filters. Default true.
Default:
array()
Source
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 );
}
Hooks
- apply_filters( ‘wp_get_nav_menu_items’,
array $items ,object $menu ,array $args ) Filters the navigation menu items being returned.
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Building bootstrap 3 menu with submenu items without use WP_nav_walker (boostrap)! (Require bootstrap.css and bootstrap.js)
if( $menu_item->menu_item_parent == 0 )Otherwise there will be several closing li tags not closing anything for all submenu items since they have parents and won’t pass this check.Building menu list with children (submenus) and selecting the menu by it’s location:
Get simple array of menu.
function wp_get_nested_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; $submenu[$m->ID]['parent'] = $m->menu_item_parent; if (isset($submenu[$m->menu_item_parent])) { $submenu[$m->menu_item_parent]['children'][$m->ID] = $submenu[$m->ID]; $mainparentid = $submenu[$m->menu_item_parent]['parent']; $menu[$mainparentid]['children'][$m->menu_item_parent] = $submenu[$m->menu_item_parent]; }else{ $menu[$m->menu_item_parent]['children'][$m->ID] = $submenu[$m->ID]; } } } return $menu; }Building simple menu list
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]
I have tested this menu , it supports sub menu , submenu will have arrow , also current page parent and menu item will have active class.
Here are two functions that can be used to add the menu to the REST API and retrieve individual menu items:
This allows us to access both:
https://example.com/wp-json/custom/menuandhttps://example.com/wp-json/custom/menu/{menuID}If you’re having issues with this function returning empty arrays since version 6.0 (as i did) make sure you’re not accidentally adjusting the
$query, for example by setting thepost_typeto a custom type in apre_get_postsaction.Build 3 level hierarchical menu
Here’s a simple recursive solution to get an array of a menu and all submenus: