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

Comments

styro’s picture

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

binford2k’s picture

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 ...

k.obrien’s picture

  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!

marc82’s picture

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

nunami’s picture

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:

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

encho’s picture

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?

paulnem’s picture

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

baronmunchowsen’s picture

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.

ebsqart’s picture

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:

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):

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

baronmunchowsen’s picture

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.

ebsqart’s picture

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 } ?>
    
oskar_calvo’s picture

Hello ebsqart

This code works for drupal 5.x?

thanks

Oskar

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

rpardun’s picture

As another post on here stated, the ability to get the Menu ID# doesn't work in 6.15 so I assume this is a 5.x solution. Working with the code in the above posts (from k.obrien) and it seems to work.

So does anyone know of a way to implement this complete solution in 6.x or is it an entirely different beast now?

Thanks

U8IK’s picture

For Drupal 6 you will use the machine-readable name you assigned to your new menu (so you need to remember it).
But you need to add 'menu-' in front of it. So if your new menu's name is 'custom-menu' you'll have to use: print theme('links', menu_navigation_links('menu-custom-name'));

see http://drupal.org/node/284255

helichrysum’s picture

If you want to create custom menu layout, you can use folowing code within page.tpl.php:

foreach($secondary_links as $key => $value) { echo $key."   "; print_r($value); echo "<br>";}

After that, you'll see contents of the array $secondary_links and will be able to create custom layout.