Community Documentation

How to display sub menus (children) of primary links in Drupal

Last updated December 14, 2012. Created by arshadcn on January 15, 2010.
Edited by figaro. Log in to edit this page.

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

Check out menu block module

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

Set the value of "Starting

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

Sub menus

Hi, tks for that, whole Worked.

This is great. Is there a way

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

Yes, you can

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.

<?php
/**
* 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']);
      }
     
// Dirty removal, could be cleaned up a lot.
     
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,

Avoiding an error in case there are no submenus (yet)

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 = '';

Correct way for Drupal 7

<?php
$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;
?>
nobody click here