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:
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:
$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:
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.)
$header_links = menu_navigation_links(‘menu-header-links’);
print theme(‘links’, $header_links);
?>
