Categories: Wordpress Development

Get Menu ID by Theme Location

Sometimes we have to build up our menu settings by own HTML layout where wp_nav_menu or nav-menu-walker function becomes harder than our capability. To avoid those kind of situation we like to havewp_get_nav_menu_items($menuID); where if you pass the ID you will get a set of array-object to display menu layout in front nicely.

Now, question how we will get the ID to get the set of menu-list. Here is my tutorial for you to get that Menu ID by theme location.

Formula-1: If you want only the ID the simply follow below function-

$theme_location = 'main-navigation';//your theme location name

$locations = get_nav_menu_locations();
$menuID = $locations[$theme_location];

Formula-2: If you need menu ID, menu name, menu slug, etc then you can use below function

$theme_location = 'main-navigation';//your theme location name

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

$menuID =$menu_obj->term_id;
$menuName =$menu_obj->name;
$menuSlug = $menu_obj->slug;
$menuCount = $menu_obj->count;

 

 

Formula-3: If you want to get all Menu Information that has been created by users through admin menu settings, then you can follow below code

$menus = get_terms( 'nav_menu' );
foreach ( $menus as $menu ) {
 $menuID =$menu->term_id;
 $menuName =$menu->name;
 $menuSlug = $menu->slug;
 $menuCount = $menu->count;
}

 

Formula-4: If you want to get Menu ID by Menu Name that has been created by users through admin menu settings, just follow simple rules

get_term_by( $field, $value, $taxonomy, $output, $filter );//codex function

//Suppose our menu name is 'Social Network'

$term = get_term_by('name', 'Social Network', 'nav_menu');
$menu_id = $term->term_id;

That’s all. Still if you face any problem to get your solution. Feel free to drop a message, hope we can sort out your problem.

Thanks.

This post has already been read 7334 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

Share
Published by
Mehedi Hasan
Tags: menu

Recent Posts

WP Share A Friend – A Way to Capture Lead

What is Lead Capture: In general sense lead capture designates a process or way of…

5 years ago

Unlimited Modal POPUPS on MouseClick

Introduction Unlimited Modal POPUPS on MouseClick: Everyone knows about popup during site visits and most…

5 years ago

WordPress Pregnancy Calculator Plugin

Brief about Pregnancy Calculator : This WordPress Pregnancy Calculator plugin allows users to display an…

5 years ago

Interactive Service Plugin with Hover Effects VC

Introduction About this Service Plugin: This Visual Composer Service Plugin works as an addon to…

6 years ago

WP Dynamic Query String

Introduction of Dynamic Query String : This plugin allows user to set dynamic query string…

6 years ago

How to make scroll PDF in iFrame for iPAD Safari

There is an occasional bug for iPhone and iPad even they are designed as well and…

7 years ago