I need to get the title of my top-level navigation. I already know the ID of the top-level menu-item but i need to get its title. How can one do that?
I currently have menu like this:
If im on any of the level 2 pages i have the Level 1 menu-item ID. I need to get the title in the current theme-location instance.
<?php if(getTopSelectedMenu('primary')):
$topParent = intval(getTopSelectedMenu('primary'));
$topParentTitle = "";
?>
<div class="sidebar-menu-headline"><?=$topParentTitle?><i class="fa fa-arrow-down" aria-hidden="true"></i></div>
<?php
$args = array(
'menu' => '',
'menu_class' => 'sidebar-menu',
'container' => 'ul',
'theme_location' => 'primary',
'level' => 2,
'child_of' => $topParent,
'echo' => FALSE,
'fallback_cb' => '__return_false'
);
$submenu = wp_nav_menu( $args );
if ( ! empty ($submenu) )echo $submenu;
?>
use the below function to get menu title by passing menu id.
Function: wp_get_nav_menu_object($menu) // $menu can be 'id','name' or 'slug'
Returns : Object (
term_id => 4
name => My Menu Name
slug => my-menu-name
term_group => 0
term_taxonomy_id => 4
taxonomy => nav_menu
description =>
parent => 0
count => 6
)
// in your case.
$menu = wp_get_nav_menu_object($topParent );
$menu_title = $menu->name;
You can find more information here. https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_object
To get the Menu title, Use the below code
if(getTopSelectedMenu('primary')){
$topParentId = intval(getTopSelectedMenu('primary'));
$menuLocations = get_nav_menu_locations(); // Get our nav locations (set in our theme, usually functions.php)
// This returns an array of menu locations ([LOCATION_NAME] = MENU_ID);
$menu = wp_get_nav_menu_object($menuLocations['primary']) ; // Get the *primary* menu object
$primaryNav = wp_get_nav_menu_items($menu->term_id); // Get the array of wp objects, the nav items for our queried location.
foreach ( $primaryNav as $navItem ) {
if ($navItem->ID == $topParentId ) {
$topParentTitle = $navItem->title;
}
}
}
<div class="sidebar-menu-headline"><?=$topParentTitle?><i class="fa fa-arrow-down" aria-hidden="true"></i></div>