Tertiary menus in Drupal

In keeping with the theme of the previous post, here is another fantastic tip for pulling out more levels of your primary links menu.

By default, Drupal gives you primary and secondary links. These can come from the same menu tree; primary links will be the first level, and secondary links will be the second level. But what happens when you want the third level? Drupal does not give you this variable by default.

Tony Mobily from Free Software Magazine shows us the simple way to get at this third menu level.

Basically, he uses Drupal's way to generate secondary links to pull out the tertiary links:

menu_primary_links(2, variable_get('menu_primary_menu', 0) );

By changing the number 2 to 3, we will get the tertiary links. In theory, I think we can probably do more levels i.e. the fourth, fifth, etc. menu levels, too, just by increasing that number. I haven't tried this out, so don't quote me on that.

So to print out your tertiary links (in page.tpl.php for example), use the following code (ideally, I would recommend creating a new variable in the _phptemplate_variables() function):

<?php
$m
= menu_primary_links(3, variable_get('menu_primary_menu', 0) );
print
theme('links', $m, array('class' => 'links tertiary-links'));
?>

Yes, its really that simple! Amazing, eh?

Posted in:

Update for Drupal 6

That was a great tip.

Actually, this method does not work in D6, but it only needs a minor change. Instead of this:

$m = menu_primary_links(3, variable_get('menu_primary_menu', 0) );

we should use:

$m = menu_navigation_links('primary-links', 2 );

Note the number 2 instead of 3. Menu deepness starts counting from zero instead of 1, so for tertiary links we must write "2".

Other than that, this tip saved my day. Thanks!

tertiary-menu display positioning wrong

Hi,
When using the method in my website the menu-display is as follows:

  • item A
  • item B
  • item C
  • subitem of B
  • subitem of B
  • subitem of B

instead of the desired:

  • item A
  • item B
    • subitem of B
    • subitem of B
    • subitem of B
  • item C

Do you have any idea what I might have done wrong?
tnx in advance.

That's very odd. The only

That's very odd. The only thing I can think of is that your theme might have some override that modifies the theme_links function. Check your theme's template.php file for a function called yourthemename_links().

magnanimous-junior