Loading a Menu, and Theming the Links in Two Lines of Code

10.10.2008

When creating a menu [ admin/build/menu/add ] there's a field for "Menu name". We use that value to load whatever array of menu links we want. In this example, we're using "secondary-links" which comes default on most drupal installs.

<?php

  $menu = menu_navigation_links("secondary-links");
  return
theme('links', $menu);
?>

Discussion

This is a particularly nice technique to use along with hook_preprocessor_page. You can easily send your menu links to your page.tpl.php file from a module like this:

<?php
function hook_preprocess_page(&$vars) {
 
$menu = menu_navigation_links("secondary-links");
 
$vars['footer_menu_primary'] = theme('links', $menu);
}
?>

The array return by menu_navigation_links is also flexible, giving you both a title and an href. So really, you can do whatever you want with these links once you grab em. This technique is far better than say, hard coding links in a function like many of us did in the dark days of drupal past.

Comments

Thanks for this! This will be

Thanks for this!

This will be useful today, as I've got to load all the secondary links menus of all the primary links, so that I can implement a jquery show/hide on mouse over for each menu. Hopefully, this snippet will get me going in the right direction, without having to severely hack into the core menu functions in template.php.

I may end up having to create additional menus and remove them as secondary links nested within the primary navigation menu... crossed fingers and coffee.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options