X

Get Assigned Menu Location with get_nav_menu_locations

Snippets by IsItWP

Do you want to get the name of an assigned menu based on the location name? While menu settings can be managed in appearance settings, we have created a quick code snippet that you can use to get assigned menu location with get_nav_menu_locations in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function mf_get_menu_name($location){
    if(!has_nav_menu($location)) return false;
    $menus = get_nav_menu_locations();
    $menu_title = wp_get_nav_menu_object($menus[$location])->name;
    return $menu_title;
}

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: How to set up author tracking in WordPress with Google Analytic and 15 best content marketing tools.

Comments   Leave a Reply

  1. Menno van der Krift January 2, 2018 at 5:22 am

    Not doing anything for me. Just getting a white page now.

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!