Displaying a Custom Menu

baronmunchowsen - January 25, 2007 - 17:52

I have created a custom menu - called Custom Links

I want to display this menu in the same fashion as my primary links.

From my page.tpl.php file I can see that this is done as follows:

<?php if (isset($primary_links)) { ?><div id="primary"><?php print ('links', $primary_links) ?></div><?php } ?>

however, when I try the following, nothing appears.

<?php if (isset($custom_links)) { ?><div id="custom"><?php print theme('links', $custom_links) ?></div><?php } ?>

I can turn on the block that the menu module has created - 'Custom Links' - but this doesn't present the links in the same way as the primary links, and not in the place I want them tobe on the page. It presents them as an unordered list, rather than with a delimiter.

Can anyone let me know how to do this - I'm sure it's simple enough - I just have no clue!

Best Regards

PHPTemplate variables

styro - January 25, 2007 - 18:10

From what I just wrote about them here:
http://drupal.org/node/112726

This is how the variables are set in page.tpl.php (for 4.7)
http://api.drupal.org/api/4.7/function/phptemplate_page

    'primary_links'       => menu_primary_links(),
    'secondary_links'     => menu_secondary_links(),

You can add your new $custom_links variable by following these instructions:
http://drupal.org/node/16383

And from:
http://api.drupal.org/api/4.7/function/menu_primary_links

you could do something like:

$vars['custom_links'] = menu_primary_links(1, $your_custom_menu_id);

in your template.php file.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ

menu_primary_links() would

binford2k - October 22, 2007 - 05:20

menu_primary_links() would be so much more useful to me if I could pass in the name of a custom menu instead of digging to find out the id to use ...

I used this solution with Drupal 6.3

k.obrien - July 24, 2008 - 18:16
  1. Go to admin/build/menu.
  2. Create the new menu by clicking "Add Menu".
  3. Remember the machine readable name you type in - you will need this to make it appear in step 6.
  4. Add your menu items.
  5. Open your page.tpl.php file and go to the spot where you want your menu to appear.
  6. Enter the following code replacing "machine-readable-name" with the one you entered in step 2 (the catch is that Drupal adds "menu-" in front of whatever you typed in, so you must leave "menu-" in front of your machine readable name). Then substitute your own id or classes.

    <?php print theme('links', menu_navigation_links('menu-machine-readable-name'), array('id' => 'menu_id', 'class' => 'menu_class')); ?>

That's all I did and it worked perfectly. Hope it helps!

thank you

marc82 - August 17, 2008 - 15:58

don't know about the others, but perfect for my specific problem. thanks :)

King among Men

nunami - January 8, 2009 - 01:03

You sir are a king among men! I've been looking into this for the past few days and finally came across your post.

I have been wanting to make a custom menu my primary menu. I have a segmented my site into mini-sites using sections and path-alias. With all that set up, i wanted each section to have it's own menu displayed as the primary menu. Using the code above i changed the last bit to the following:

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

My custom theme now displays a custom menu for the specific section!

Thanks again

- nunami

anyone?

encho - February 23, 2008 - 14:50

I am looking for this as well. Looks like something very simple. So how to include that custom menu as part of your theme? And how to find menu id in Drupal 6?

Sorry, bumping this thread.

paulnem - February 27, 2008 - 22:58

Sorry, bumping this thread. I'm wondering the best way (drupal 5.7) to include a custom menu into the page.tpl.php?

As per the first response to

baronmunchowsen - March 13, 2008 - 16:56

As per the first response to my original post - use template.php to define the menu and then include it where you like in your page.tpl.php file.

To find the menu id# go to edit the menu and hover over the link. Look for the id# in the link.

Hope this helps.

Hey - that does work!

ebsqart - March 26, 2008 - 08:16

I did a quick test - created a new menu (got the id# for it by hovering over edit link - 158 in my case)...

In my theme's page.tpl.php file, as a test, I swapped out an original menu:

<?php
print theme('links', $secondary_links, array('class' => 'links secondary_links'))
?>

with a new one... I didn't create a variable, just called menu_primary_links where $secondary_links used to be... also, supplied my own class info (supportliving_links):

<?php
print theme('links', menu_primary_links(1, 158), array('class' => 'links supportliving_links'))
?>

(and, after the test, I was able to display all three menus).

Can anyone suggest if (and if so, where) I should create a new variable for such a menu?

i.e.: myvar = menu_primary_links(1, 158);

Thanks,

bill

Create this in your

baronmunchowsen - March 26, 2008 - 20:15

Create this in your template.php file:

function _phptemplate_variables($hook, $vars) {
  $vars = array();
  if ($hook == 'page') {
    $vars['myvar'] = menu_primary_links(1, 158);
  }
  return $vars;
}

should do the trick.

Yes - that did the trick

ebsqart - March 28, 2008 - 08:22

Thanks...

In Summary - To add additional menus beyond primary and secondary:

  • Create the new menu (/admin/build/menu/menu/add)
  • Note the new menu's ID (/admin/build/menu) - hover above the 'edit' link for new menu (ex: mine was 158: /admin/build/menu/menu/edit/158)
  • Update the function _phptemplate_variables template.php as mentioned above by baronmunchowsen - replacing 'myvar' with a likely unique variable name of your own. (Be sure to replace YOURMENUID with your menu id.) Read http://drupal.org/node/207841 for help with this function.
    function _phptemplate_variables($hook, $vars) {
      $vars = array();
      if ($hook == 'page') {
        $vars['myvar'] = menu_primary_links(1, YOURMENUID);
      }
      return $vars;
    }
  • Update your theme's page.tpl.php, inserting the below code into the location you wish your menu to appear (replacing $myvar with the name you used for your variable - it appears twice.) Also, the two divs have an id/class that I assigned - Look for the code for the other menus for help with that (it seems the id for the div before 'column' matches the 'class' assigned in the called 'theme' function (in my case, both named 'supportliving_links'... seems one is an id and the other a class - so not sure if they have to match.)
        <?php if (isset($myvar )) { ?>
          <div id="supportliving_links">
            <div class="column">
              <?php print theme('links', $myvar , array('class' => 'links supportliving_links')) ?>
            </div>
          </div>
        <?php } ?>

Hello ebsqart This code

oskar_calvo - May 4, 2008 - 08:25

Hello ebsqart

This code works for drupal 5.x?

thanks

Oskar

gestión del conocimiento y de la información con software libre

 
 

Drupal is a registered trademark of Dries Buytaert.