gmaggio
7/26/2014 - 3:25 AM

[Wordpress] Get the Menu Name of the current page

[Wordpress] Get the Menu Name of the current page

<?php
/**
 * Get the Menu Name of the current page
 * 
 * $loc is the location name of the nav menu
 *
 * Source:
 * http://wordpress.stackexchange.com/a/155833/1044
 *
 */
function my_get_menu_item_name( $loc ) {
    global $post;

    $locs = get_nav_menu_locations();

    $menu = wp_get_nav_menu_object( $locs[$loc] );

    if($menu) {

        $items = wp_get_nav_menu_items($menu->term_id);

        foreach ($items as $k => $v) {
            // Check if this menu item links to the current page
            if ($items[$k]->object_id == $post->ID) {
                $name = $items[$k]->title;
                break;
            }
        }

    }
    return $name;
}