So- you have a view created, and you have a block or page display for the view. But now you want to add a few custom links, and you *don't* want to hard code them. Why do you want Drupal to handle them? Some advantages are that Drupal can recognize when a link is 'active' and add the proper CSS for the link; and it can also recognize if Drupal is installed in a sub directory and have a base_url in your settings.php and it will render the links properly.

This how-to assumes you have the view already created and that you have the custom menu. Visit your admin/build/menu/add and admin/build/views/add if you need to catch up.

  1. Enable PHP filtering.
  • The text field for header or footer allows filtered HTML and full HTML by default, but you must enable PHP input filtering to allow code in a view. In Drupal 6, this is disabled by default. Find it under admin/build/modules listed under 'core'. Tick [x] PHP filter, and scroll down to save configuration.
  • Now add the code to display the menu in the view
    • Edit your view, admin/build/view...
    • Look in the 2nd column Under "Basic Settings" click - Footer: None
    • Scroll down to the text field below "Defaults: Footer", and paste in this code:
                <?php print theme('menu_tree', menu_tree('menu-name')); ?>
              
    • Where is says 'menu-name' - change name to your menu name. For example 'menu-addimage', where addimage was the name of the menu.
    • Note: When you made the custom menu, Drupal appended "menu-" to the name, so look at the path in the admin/build/menu area to double check the name.
    • Under, click Input format, select "PHP code".
    • Click update.
    • Then save. You should see your menu now.
  • FYI: API reference in case someone is searching: http://api.drupal.org/api/function/menu_tree/6
    function menu_tree

    Credits: Thanks to NikLP and JohnAlbin who helped sort this out!

    Comments

    ghazlewood’s picture

    While this approach worked for me in theory looking at the output of the page I was getting a double ul which was messing things up. e.g <ul class="menu"><ul class="menu>

    Using the below snippet from http://agaric.com/note/printing-arbitrary-menu-just-primary-links-and-se... fixed the problem

    $menu = menu_navigation_links("menu-name-here");
    print theme('links', $menu); 
    

    Hope this helps someone else!

    vucuongkg’s picture

    Thank you very much! It is very helpful for me! And I have a tip. You can using phpmyadmin to get the name of menu. You will see name in "menu_custom" table (menu_name column).

    nobarte’s picture

    Simply copy the name of menu from its url, e.g. admin/build/menu-customize/menu-name

    Anonymous’s picture

    Helped very much, thank you!

    MediaFormat’s picture

    Hi this was almost exactly what I was looking for...

    But the menu class shows up as <ul class="links"> instead of menu,
    I tried replacing 'links' in the php to 'menu' but to no avail...
    anyone else have this problem, or its solution?

    Thanks

    -D

    MediaFormat’s picture

    found in another post:

    <?php
    print theme('links', menu_navigation_links('MENU-NAME'), array('class' => 'menu custom-class'));
    ?>

    This permits adding custom css classes

    cheers

    jaumepc’s picture

    Sencill, i eficient. funciona en tots els templates també. gracies.

    Simple and efficient, works in all templates too. thank you.

    Chris Pergantis’s picture

    I know that this post might be older than the release I am using as mentioned in the subject line of my comment. I find no area to place any code when I either go to the header or the footer as described above. We are required by other client needs to be in version 3.

    Is this deprecated in this version or is it just that the header and footer areas of the form has not been ported into version 3 yet?

    Am I missing something in the post?

    Any help is most appreciated.

    Chris Pergantis
    Founder/Chairman/President
    Accelerated Design Inc

    fehin’s picture

    subscribing

    perisdr’s picture

    The above was not working for me. After googling a little a stumbled upon this

    So this is the magic line that will do it for you

        print theme('links', array('links' => menu_navigation_links('main-menu'), 'attributes' => array('class'=> array('links', 'main-menu')) ));