wp_get_nav_menu_object()
Gets the WordPress menu object. Returns only the menu object itself without the items in it.
Use wp_get_nav_menu_items() to get the menu items (elements).
Uses: get_term(), get_term_by()
Used By: is_nav_menu(), wp_get_nav_menu_items()
1 time — 0.000026 sec (very fast) | 50000 times — 0.24 sec (very fast)
Hooks from the function
Returns
WP_Term|false
.
false
- failed to get the menu by the parameter $menu, the menu (term) does not exist.-
WP_Term - taxonomy term object (menu). Menus are stored as taxonomy terms
nav_menu
.WP_Term Object ( [term_id] => 693 [name] => Panel [slug] => panel [term_group] => 0 [term_taxonomy_id] => 701 [taxonomy] => nav_menu [description] => [parent] => 0 [count] => 1 [filter] => raw )
Usage
wp_get_nav_menu_object( $menu );
- $menu(int/string/WP_Term) (required)
ID, slug, or name of the menu.
It is possible to pass the menu object directly, in this case it will be passed through the filter wp_get_nav_menu_object and returned.
Examples
#1 Demo
// By name. $menu = wp_get_nav_menu_object( 'wpdocs mainmenu' ); // By slug. $menu = wp_get_nav_menu_object( 'wpdocs-mainmenu' ); // By ID. $menu_name = 'wpdocs mainmenu'; $menu_obj = get_term_by( 'name', $menu_name, 'nav_menu' ); $menu_id = $menu_obj->term_id; $menu = wp_get_nav_menu_object( $menu_id ); // By location. $menu_name = 'primary'; $locations = get_nav_menu_locations(); $menu_id = $locations[ $menu_name ] ; wp_get_nav_menu_object( $menu_id );
#2 Get the menu object
Suppose we have a menu location name: my_menu_location
registered with register_nav_menu(). And we created a menu in the admin and attached it to that location. Then:
$locations = get_nav_menu_locations(); if( isset( $locations['my_menu_location'] ) ){ $items = wp_get_nav_menu_object( $locations['my_menu_location'] ); print_r( $items ); /* will output stdClass Object ( [term_id] => 693 [name] => Panel [slug] => panel [term_group] => 0 [term_taxonomy_id] => 701 [taxonomy] => nav_menu [description] => [parent] => 0 [count] => 1 [filter] => raw ) */ }
Changelog
Since 3.0.0 | Introduced. |