The blog

Loading a menu directly from a template in Drupal: menu_navigation_links

I had a difficult time trying to get Drupal to load a menu directly from a template file, such as page.tpl.php.

I had a menu titled ‘Header Links’. This is not one of the default menus included with drupal. Those are Navigation, Primary Links, and Secondary Links, and by default, these three menus are available to the theme templates. Using the theme to load the menu is a way to ensure your CMS admin doesn’t accidentally remove the menu via the blocks page.

I found an article on Nick Lewis’ blog, Loading a Menu, and Themeing the Links in Two Lines of Code. Nick mentions a function to accomplish this:

1
2
3
<?php
menu_navigation_links($menu_name, $level = 0) {...}
?>

Apparently we should be able to call this function, which will return an array of links for a navigation menu. However, I couldn’t get it to load.

When I created my Header Links menu, I named it exactly ‘header-links’. So I’d figured that calling this from the template:

1
2
3
4
<?php
$header_links = menu_navigation_links('header-links');
print theme('links', $header_links);
?>

would output the correct menu. But it didn’t. Even a print_r() call on it returned nothing, just an empty array.

Finally after a long time, I came across a function that was worth a quick shot:

1
2
3
<?php
menu_get_menus($all = true) {...}
?>

After calling that guy, I was shown a list of all my menus. And guess what? Drupal named my ‘header-links’ menu ‘menu-header-links’ instead of what I named it. Ok, that’s fine. I understand.

So finally, calling this code actually worked! (Note the use of ‘menu-header-links’ for the menu name.)

1
2
3
4
<?php
$header_links = menu_navigation_links('menu-header-links');
print theme('links', $header_links);
?>

8 Responses to “Loading a menu directly from a template in Drupal: menu_navigation_links”


  1. Steve
    2008.12.14

    This was regarding Drupal 6.x


  2. Andrew Ariotti
    2009.03.17

    So here’s another question for you. I like what you’ve done here, but I’d like to ask another question.

    How would I print a specific part of a menu?

    Here’s a better explanation of what I’m looking for.

    http://drupal.org/node/403934


  3. Steve
    2009.03.17

    Have you gone through the menu api?

    http://api.drupal.org/api/group/menu/6


  4. Nigel
    2009.03.26

    You can also figure out your menu name by going to Site Building > Menus > Navigation and then clicking configure for the menu you want. First field ends with your menu name


  5. Endre Palfi
    2009.07.23

    This was very helpful although it didn’t work at first because you used Grave Accents instead of Apostrophes in the following code and PHP didn’t like it:

    $headerlinks = menunavigation_links(‘menu-header-links’);

    PHP is sooooooo sensitive :)


  6. Steve
    2009.07.24

    Yeah, it’s the wysiwyg that adds the curly quotes… at least you didn’t have to retype more than one line… ;)


  7. Rod Miles
    2010.01.26

    Thanks Steve… been looking around for a bit for a good source on this. You rock!


  8. Josh
    2010.04.05

    Thanks for this post. I’ve been struggling with it for over an hour, but this did the trick.

Leave a Reply