Skip to content

Instantly share code, notes, and snippets.

@WisdomSky
Created April 20, 2015 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WisdomSky/1bd5a0382c9f1a089ff7 to your computer and use it in GitHub Desktop.
Save WisdomSky/1bd5a0382c9f1a089ff7 to your computer and use it in GitHub Desktop.
Sorts the results returned by Wordpress' wp_get_nav_menu_items() function into a hierarchichal multi-dimensional array.
/*
* you can use this function just like the way you use wp_get_nav_menu_items() in wordpress
*
*/
function wp_get_nav_menu_items_hierarchy($menu,$args=null){
$arr = array();
function _inject_child($id,$item,&$list){
if($id==0){
$list[] = $item;
} else {
foreach($list AS &$child){
if($child->ID==$id){
if(!isset($child->children)){
$child->children = array();
}
$child->children[] = $item;
} else {
if(isset($child->children) && count($child->children)>0){
_inject_child($id,$item,$child->children);
}
}
}
}
}
foreach (wp_get_nav_menu_items($menu, $args) AS $menu_item){
_inject_child($menu_item->menu_item_parent, $menu_item, $arr);
}
return $arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment