Warning: include(/home/pixenpaa/wphats.com/wp-content/advanced-cache.php): failed to open stream: No such file or directory in /home/pixenpaa/wphats.com/wp-settings.php on line 95

Warning: include(/home/pixenpaa/wphats.com/wp-content/advanced-cache.php): failed to open stream: No such file or directory in /home/pixenpaa/wphats.com/wp-settings.php on line 95

Warning: include(): Failed opening '/home/pixenpaa/wphats.com/wp-content/advanced-cache.php' for inclusion (include_path='.:/opt/alt/php72/usr/share/pear') in /home/pixenpaa/wphats.com/wp-settings.php on line 95
Get Menu ID by Theme Location » WP Hats

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *