Get the menu name or location of current menu item

I am having a hard time trying to get the name of the active menu and, apparently, I cannot find any solutions to this issue. Basically, I have several menus that follow this pattern:

Menu Name

Read More
  • Menu Item 1
  • Menu Item 2
  • Menu Item n

I want to display the menu name of the current page item, but I cannot find any way of doing it. I have tried using get_nav_menu_locations() in conjunction with get_term():

$theme_locations = get_nav_menu_locations();
$menu_obj = get_term($theme_locations["menu_1"], 'nav_menu');
$menu_name = $menu_obj->name;

However, I need to get the location of the current menu dynamically and after searching for answers/solutions and not finding anything concrete, I just assume that there may be at least a database relationship between the current menu item and the menu object to which is assigned; from where I would eventually get the menu location/term ID.

PS: I want to output the active menu title without the use of JS

Related posts

Leave a Reply

1 comment

  1. I coded a solution to highlight the current active hyperlink in a menu within WordPress. It’s a simple PHP snippet that checks to see if you are on a specified page ID.

    Example #1:

    <?php if (is_page(2) || is_page(25)){ ?> OUTPUT CLASS HERE, MENU NAME (Menu Item 1, Menu Item 2) OR ANYTHING ELSE<?php } ?>
    

    Example #2 (How I used it):

    <li class="nav_b<?php if (is_page(2) || is_page(25) || is_page(27) || is_page(30) || is_page(31)){ ?> current_page_item<?php } ?>"><a rel="external nofollow" class="app_tickets<?php if (is_page(2) || is_page(25) || is_page(27) || is_page(30) || is_page(31)){ ?> active<?php } ?>" href="/example-url/" onClick="_gaq.push(['_trackEvent', 'Tickets', 'Click', 'B', 1, false]);" title="Example Title">Example Hyperlink</a></li>
    

    You can easily modified it to output the menu’s name!