1

How to get ONLY the list of Submenus under given menu programmatically. Say, the given menu means a main-menu item. I tried with menu_tree_page_data. But not sure if I can use it to select only the required menus.

The scenario is, I have active menu loaded using menu_get_active_trail(). So I have all the details of the main menu. Now with those details I need to get the submenus under that.

1
$main_menu_tree = menu_tree('main-menu');
foreach ($main_menu_tree as $key => &$main_menu_item) {
    if (is_numeric($key)) {
        $main_menu_item['#below']['#theme_wrappers'][0] = 'some_other_theme_wrapper';
    }
}
print drupal_render($main_menu_tree);

If you have array of main menu active trails you can use foreach to loop through the array print menus and their sub menus.

0

Try this code and it should work. I tested it now and it works by fetching sub menus.

$menuItems = menu_tree_page_data('main-menu');
foreach($menuItems as $key => $value) {
    foreach($value as $menuKey => $submenus) {
        if(isset($submenus['link_title'])) {
            print $submenus['link_title'];
        }
    }
}
  • This will get all the Sub-menus not sub-menus only under a given menu. – Kannan Ramamoorthy Sep 25 '15 at 4:31
0

if you just a specified Menuitem subItems just put a if between 2 foreach

$menuItems = menu_tree_page_data('main-menu');
$sub=array();
foreach($menuItems as $key => $value) {
if($menuKey=='yourmenuitemkey'){
    foreach($value as $menuKey => $submenus) {
        if(isset($submenus['link_title'])) {
            $sub[]=$submenus['link_title'];
        }
      }
    } 
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.