Drupal:Displaying sub menus of primary link

23 10 2009

I want to display the sub menus of the currently accessing primary link as a block. Drupal provides Primary links, Secondary links blocks but does not display sub menus of currently accessing primary link.

When I search, I got following code snippet which lists the sub menus of currently accessing primary link. I directly created block using Add block by adding this php code snippet. It works a charm.

$tree = menu_tree_page_data('primary-links'); //substitute your menu name for primary-links
foreach($tree as $key => $mi) {
if ($mi['link']['in_active_trail'] && $tree[$key]['below']) {
$menu = menu_tree_output($tree[$key]['below']);
}
}
print $menu;

I hope this will help others to find easily.

Advertisements

Actions

Information

16 responses

24 11 2009
JonLoh

Awesome! Worked like charm 🙂 Was searching exactly for this code.

Thanks a bunch!

31 12 2009
kinza

this is great help and very much easy thanks

2 02 2010
Alon

Thanks alot !!
iv’e been searching for this command for a while, thanks again =]

15 03 2010
Roman Sharf

Cool! I have am using secondary links with the theme “giordani”. Is there a way to do this with secondary links? Thank you.

15 03 2010
Prajwala

Yes you can display sub menus of secondary links by changing the above code. In place of primary-links use seconday-links. That means code will be

$tree = menu_tree_page_data('secondary-links'); //substitute your menu name for primary-links
foreach($tree as $key => $mi) {
if ($mi['link']['in_active_trail'] && $tree[$key]['below']) {
$menu = menu_tree_output($tree[$key]['below']);
}
}
print $menu;

27 04 2010
juliawan

is it can show like drop down menu sir?

27 04 2010
Prajwala

If you want to show drop down of menu items, use nice_menus module

11 05 2010
ben

Thanks for sharing!!!
Just what I needed.

4 06 2010
pythk

Hi, guys.

That trick was really helpful. I was wondering though:

Is there any trick for keeping the primary link in focus when any child menu item is pressed?

Thank you in advance.

5 06 2010
Prajwala

Drupal by default add the class “active-trail” to the parent item. You just need to add css for that element.

25 02 2011
ruinze

good day, i’m new to drupal..where do i put the code snippet on the block? i’m using drupal 6 and the wabi theme…thnx in advance

27 02 2011
Prajwala

Yes, you have to keep the code in block. From drupal admin interface go to add block page and past the code and choose the input format as php-mode.

23 09 2011
Revathi

Could you please tell me for which theme this code works.
Thanks

24 09 2011
Prajwala

Yes, it will work for any theme

13 03 2012
ShaoKhan

How it is with 2 Submenu’s from primary links.

13 03 2012
Prajwala

For sub menus the best option is menu block module.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: