This page describes how to display sub menus (children) of primary links in Drupal.
Using Menu Block
- Download and install the Menu Block module.
- Create a new menu block.
- 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.
This would be invoked by defining the new menu block, using:
hook_block_info();
hook_block_view($delta = '');
Hope this helps,
Could you give me a tutorial
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)
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
This is my website in drupal
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
Where to place the script
Where to place the script? on what file?
Even simpler:
This is all you need to render level 2 of
menu-mymenu
:Or if you'd like to get more advanced: