Skip to content

Instantly share code, notes, and snippets.

@bespr
Last active December 17, 2015 12:39
Show Gist options
  • Save bespr/5610955 to your computer and use it in GitHub Desktop.
Save bespr/5610955 to your computer and use it in GitHub Desktop.
Wordpress function to return the menu label of a page
<?php
/**
* @return array - info about the page, which is only available in the menu
*/
function getCurrentMenuValues() {
global $post;
$reArray = array();
$locationSlug = 'main';
$locationArray = get_nav_menu_locations();
if (isset($locationArray[$locationSlug])) {
$menuId = $locationArray[$locationSlug];
$menuItems = wp_get_nav_menu_items($menuId);
foreach ($menuItems as $item) {
if ($item->object == 'page' && $item->object_id == $post->ID) {
$reArray[] = array('title' => $item->title, 'url' => $item->url, 'menuId' => $item->ID, 'parentId' => $item->menu_item_parent);
break;
}
}
// More Hierarchy Levels (only one level for now, I dont need more, otherwise make it recursive)
if (isset($reArray[0]['parentId']) && $reArray[0]['parentId'] > 0) {
$parentId = $reArray[0]['parentId'];
foreach ($menuItems as $item) {
if ($item->ID == $parentId) {
$reArray[] = array('title' => $item->title, 'url' => $item->url, 'menuId' => $item->ID, 'parentId' => $item->menu_item_parent);
break;
}
}
}
}
return $reArray;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment