Skip to content

Instantly share code, notes, and snippets.

@gmaggio
Created July 26, 2014 03:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gmaggio/df4f0bb9afdfe8e1f106 to your computer and use it in GitHub Desktop.
Save gmaggio/df4f0bb9afdfe8e1f106 to your computer and use it in GitHub Desktop.
[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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment