get all menu list in hierarchical wordpress

hierarchical menu in wordpress

Get all menu in hierarchical  with array elements please find out the below code.

<?php
$rows = wp_get_nav_menu_items('Menu 1',$args);
$tree = buildTree($rows);
print_r($tree);
function buildTree($elements, $parentId = 0) {
    $branch = array();
    foreach ($elements as $element) {
        if ($element->menu_item_parent == $parentId) {
            $children = buildTree($elements, $element->ID);
            if ($children) {
                $element->children = $children;
            }
            $branch[] = $element;
        }
    }
    return $branch;
}

 

Related posts