Get menu object from theme_location

The question:

I’d like to get a menu object from its theme location argument.

My goal is to output separately the menu name and its items name, url and description.

Example of what I’m looking for :

$menu = get_menu('nav-menu'); //get menu from its theme location
echo $menu->name; //displays the menu name
foreach($menu->items as $item){
    echo '<a href="'.$item->link'" rel="nofollow noreferrer noopener">'.$item->name.'</a>'; //displays a link to the item destination
    echo $item->description; //displays the item description

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

This method looks like what you’re looking for, using get_nav_menu_locations() and get_term():

$theme_locations = get_nav_menu_locations();

$menu_obj = get_term( $theme_locations[$theme_location], 'nav_menu' );

$menu_name = $menu_obj->name;

(See the link for the whole thing wrapped up in a custom function; the above code just highlights the relevant WP functions for getting what you’re after.)

Method 2

Or if you need it in one line, just copy that and replace “change-this-location-slug” by our own.

$string = get_term(get_nav_menu_locations()['change-this-location-slug'], 'nav_menu')->name;

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment