This page describes how to display sub menus (children) of primary links in Drupal.

Using Menu Block

  1. Download and install the Menu Block module.
  2. Create a new menu block.
  3. Set the value of "Starting level" to "2nd level (secondary)".

Programmatically

Step 1: Since we'll be using php codes in a block to display menu, the first thing to do is to enable the PHP Filter under modules.

Step 2: Go to administer->block->add block and create a new block. Add a description (for instance submenu). Choose PHP Code as the filter and paste the code below in the body textarea.

<?php
$menuItems = menu_tree_page_data('primary-links'); 

foreach($menuItems as $key => $m) {
      //check current active menu
   if ($m['link']['in_active_trail'] && $menuItems [$key]['below']) {
       $menu = menu_tree_output($menuItems [$key]['below']);
   }  
}

//print the menu
print $menu;
?>

Make sure you include the php delimiters in your code. Click Save.

Step 3: Place your block in a region you want. and that's it. next time you select a menu item in the primary links, the children menu items will show in this block.

You can use active-trail to style the selected menu item.

Comments

mrf’s picture

If you're looking to do this, and don't want to write any PHP take a look at menu block module.

sebby’s picture

Set the value of "Starting level" to "2nd level (secondary)"

gmangones’s picture

Hi, tks for that, whole Worked.

PomGilgo’s picture

This is great. Is there a way to print the child items' parent (and just its parent not the entire parent menu)?

swim’s picture

Yes you can, please don't place this inside a PHP filter however. Managing code this way is ridiculous...

Note: this is for Drupal 7.

/**
 * Provides child menu & active parent of active parent...
 */
function MODULENAME_active_child_menu_block() {
  $menu_items = menu_tree_page_data('MENU-NAME'); 

  foreach($menu_items as $key => $m) {
    if ($m['link']['in_active_trail'] && $menu_items[$key]['below']) {
      if (isset($m['below'])) {
        $menu = menu_tree_output($menu_items[$key]['below']);
      }
      foreach ($menu as $key => $rmenu) {
        if (empty($menu[$key]['#below'])) {
          unset($menu[$key]);
        }
      }
    }
  }

  return drupal_render($menu);
}

This would be invoked by defining the new menu block, using:
hook_block_info();
hook_block_view($delta = '');

Hope this helps,

falent’s picture

Could you give me a tutorial to this solution or could you explain me step by step how I should to do it? I tried to solve my problem whole weekend :(

Now I have done 2 menu blocks with menu block module.
white menu: id="block-menu-block-6" primary menu zone (strefy)
black submenu: id="block-menu-block-8" (kandydaci)

wilbbrasil’s picture

To avoid the error of the menu variable undefined in case there aren't any submenus, I suggest it'd be defined by assigning an empty string to it before detecting the submenus. In other words: just add the following line before the foreach loop.

$menu = '';

drugget’s picture

$menu = '';
$menuItems = menu_tree_page_data('main-menu'); 
foreach($menuItems as $key => $m) {
   if ($m['link']['in_active_trail'] && $menuItems[$key]['below']) {
       $menu = render(menu_tree_output($menuItems[$key]['below']));
   }  
}
print $menu;
falent’s picture

This is my website in drupal 7. The page at http://150.254.36.80/wige/kandydaci/aktualnosci shows the following header.

I would like that "kandydaci" in this white menu will be active (black lighted) in every page in this zone, for example on http://150.254.36.80/wige/kandydaci/o-wydziale.

We are in the "kandydaci" zone, but the white menu doesn't show it.

The primary menu is "strefy" white menu
The secondary menu is"kandydaci" black menu

kangojie’s picture

Where to place the script? on what file?

timofey’s picture

This is all you need to render level 2 of menu-mymenu:

print theme('links', array('links' => menu_navigation_links('menu-mymenu', 1)));



Or if you'd like to get more advanced:

$menu = "menu-mymenu"; // your menu name
$level = 2; // menu level to print
$attributes = array('class' => array('links', 'site-menu'));
print theme('links', array('links' => menu_navigation_links($menu, $level-1), 'attributes' => $attributes));