Skip to content

Instantly share code, notes, and snippets.

@kirandash
Created July 29, 2016 09:03
Show Gist options
  • Save kirandash/434df707863f73a5627353977d52c7eb to your computer and use it in GitHub Desktop.
Save kirandash/434df707863f73a5627353977d52c7eb to your computer and use it in GitHub Desktop.
<?php
//functions.php file function
function get_menu_parent_ID($menu_name){
if(!isset($menu_name)){
return "No menu name provided in arguments";
}
$menu_slug = $menu_name;
$locations = get_nav_menu_locations();
$menu_id = $locations[$menu_slug];
$post_id = get_the_ID();
$menu_items = wp_get_nav_menu_items($menu_id);
$parent_item_id = wp_filter_object_list($menu_items,array('object_id'=>$post_id),'and','menu_item_parent');
$parent_item_id = array_shift( $parent_item_id );
function checkForParent($parent_item_id,$menu_items){
$parent_post_id = wp_filter_object_list( $menu_items, array( 'ID' => $parent_item_id ), 'and', 'object_id' );
$parent_item_id = wp_filter_object_list($menu_items,array('ID'=>$parent_item_id),'and','menu_item_parent');
$parent_item_id = array_shift( $parent_item_id );
if($parent_item_id=="0"){
$parent_post_id = array_shift($parent_post_id);
return $parent_post_id;
}else{
return checkForParent($parent_item_id,$menu_items);
}
}
if(!empty($parent_item_id)){
return checkForParent($parent_item_id,$menu_items);
}else{
return $post_id;
}
}
// Fire the function
$parentID = get_menu_parent_ID('main_nav');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment