A Practical Tutorial on Drupal's Menu System
In this tutorial, I will explain how to generate custom menus that are fully controlled from the menu administration page. (path: [www.yourdrupalsite.com]admin/menu). These menus can be enormously useful. The can be used for everything from custom user interfaces, to navigation menus. In addition, since they are hooked into Drupal's core, they can be dynamically updated.
In writing this tutorial, I've assumed that the reader already understands to some extent:
- Drupal Themes
- How to copy and paste
- The value of attempting something for the first time.
theme_menu_tree() = your new best friend
Alright, we're going to call a php function. O, non-php programmers do not flee in horror. PHP is easy, unless you're paying me, in which case its very difficult.
Now open up, your theme's page.tpl.php file. If you don't know where that is, its in themes/box_grey/page.tpl.php. Find the part of the page that you want to insert the new menu. Have you found it? Copy and paste this code into it:<?php $menuhtml = theme_menu_tree(666); print $menuhtml; ?>
As you, dear reader may have noticed, our menu's number is 666! I, for one, don't want to call Satan with our PHP function, so we're going to have to find a new number. As it so happens, you already have a whole set of numbers to choose from.
In your user menu, go to administrate->menus (the path is admin/menu). Now get the menu's number as described by the below visual aid:
In my visual aid's case, the menu number is 392. So we simply replace 666 with 392, a far more Christian number.<?php $menuhtml = theme_menu_tree(392); print $menuhtml; ?>
Quick note, if you just want to call your entire set of menu options insert the number 0 as opposed to whatever menu you've selected from your admin section.
Now, save the changes you've made to your page.tpl.php file. As you can see, assuming I wrote these instructions properly, and you executed them as deftly as I imagined, you now have a menu that is controlled from your menu adminsitration page. Add some new menu items, or create a new menu and insert in to your template. Go wild! Throw a party, and show your friends! I tell you, this trick is a great ice breaker when I'm talking to random girls in bars. And as always, if you have any problems with these instructions, leave a comment, and I'll be happy to help.