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

This looks great but where do

This looks great but where do i paste it? I'm new to drupal and i'm creating my own theme I want to customize my menu links

I am trying to get the sub

I am trying to get the sub menu items of the system navigation menu
With the code below which is given a depth 1

<?php
print_r
(menu_navigation_links('navigation', 1));
?>

It gets nothing.

How do I the whole menu tree or any level I want?

This module does the trick.

http://www.nicklewis.org/drupal-hackers-cookbook/module-guide/menu-block

Its method is kind of complicated, and I unfortunately don't know of a quick clean way to do it at this time.

RE: What about 'Added' Menus?

This works great for primary-links and secondary-links. However what if I create a new menu with the machine-name of 'keyword-links' and add items to it.

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

Does not seem to work :O???

ever find out?

ever find a fix to load menus other than primary or secondary links?

it should work so long as

it should work so long as you've copied the name of your menu properly. Maybe you're setting a variable differently in template.php then you are referring to in page.tpl.php. Or perhaps you've added a new theme hook to template.php and forgot to clear you cache?

Thought clearing the cache

Thought clearing the cache might do it, but no luck.

I've got a menu called 'Header Links', shown as 'header-links' in the URI during edit.

template.php

<?php
function hook_preprocess_page($variables)
{
 
$header_links = menu_navigation_links("header-links");
 
$variables['header_links'] = theme('links', $header_links);

  return $variables;
}
?>

page.tpl.php

<?php print $header_links; ?>

Strange... thought that would do it. Thanks for taking a moment!

by george

After a long process, I finally stumbled across a way to make this work with menus other than navigation, primary-links, and secondary-links. Posting it here for others' future reference.

I eventually came across this function, which returns a list of all the custom menus in Drupal's menu system.

menu_get_menus($all = true)

After running that guy, I was shown a list of all my menus.

I named my Header links menu as "header-links" when I first created it, but Drupal prefixed my name with 'menu-'. So the correct name of my menu was actually "menu-header-links". This took me forever to figure out!

So the following code finally worked!

<?php print theme('links', menu_navigation_links('menu-header-links')); ?>

Of course, you should probably move that over to your template.php file and send it to page.tpl as a variable, but it does work! The key is running menu_get_menus()

THANK YOU

This is amazingly helpful!! I've been searching everywhere.

Whoa! That's annoying. I

Whoa! That's annoying. I guess that function must have already been called when I was writing the example. Thanks for illuminating yet another gotcha.

newbie question

This looks great but where do i paste it? I'm new to drupal and i'm creating my own theme I want to customize my menu links

template.php. <?php$vars['wha

template.php.

<?php
$vars
['whatever_you_want_to_call_it'] = $output;
?>

becomes a straight variable in your page.tpl.php file:

<?php

print $whatever_you_want_to_call_it;
?>

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

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.