I have created a few nodes and made reference to a hierarchical "main menu":

Home
---About
------Team
------Contact
---Products
------Fruit
------Meat

When I add the "main menu" block to the sidebar (on admin/structure/block), this menu appears as it should. However, if I put print_r($main_menu); in my page.tpl.php file, it returns an empty array!

This is driving me nuts. Why is the $main_menu array empty?

B.T.W. It's not a caching problem: I have cleared all caches. Besides, everything else is working fine...aarrgh!!

Comments

chads20001’s picture

I had to use this in my templates preprocess_page to get $main_menu to work

function template_preprocess_page(&$vars) {
$vars['main_menu'] = menu_main_menu();
}

LordQuackstar’s picture

This didn't work for me in Drupal 7.

shiyanfeng’s picture

It is working well for me. Drupal 7.

Jeff Burnz’s picture

you need to run this through theme_links, as in:

 print theme('links__system_main_menu', array(
  'links' => $main_menu,
  'attributes' => array(
    'id' => 'main-menu',
    'class' => array(
      'links', 'inline', 'clearfix')
    ), 
    'heading' => t('Main menu')
  )
); 
nljeffrey’s picture

It's driving me crazy. What do I need to overwrite in template.php to remove the ul and li tags?

_menu_link
_menu_local_task

Can anyone provide an example to output a menu like this:

Home | Item1 | Item2

Or just the method to overwrite would be helpful too...

soulston’s picture

The code that @Jeff wrote will do this. You should be putting a menu in a <ul><li> format.

Just use CSS to put a border right as the inline class is already included in the array.

so something like:

li {
  border-right: 1px solid #333;
}
li.last {
  border: none;
}
abendy’s picture

Did you find a solution to this? I've tried theme_links, theme_menu_tree and theme_menu_link and none of them are working.

Jeff Burnz’s picture

theme_links is the one, lord knows why you are trying to use theme_menu_tree or theme_menu_link, they are totally different things altogether.

Look at Stark, Bartik, Adaptivetheme, Zen etc etc all these themes use theme_links one way or another.

If its not working for you, examine YOUR code.

ericxb’s picture

The variables 'main-menu' and 'secondary-menu' appear to always be defined (please correct me if my testing is wrong); however, they will only be populated with data when two things happen:

  • Your theme needs to list the features 'main-menu' and 'secondary-menu' in its .info file (or list no features at all which causes Drupal to assume they are ALL there):
    features[] = main_menu
    features[] = secondary_menu
  • The feature needs to be toggled "on" in /admin/appearance/settings. Don't forget that there are separate settings for "global" and the specific theme you are using. Check 'em both.
RedactedProfile’s picture

This solved my problem. I had no idea this would affect me at all..

armstrongpn’s picture

Code for my page.tpl.php

if ($main_menu):

endif;

Main menu has links Menu Item1 and Menu Item 2. here are no feature overrides in the .info file. $main_menu is always empty and if you switch the theme to bartik it has the same problem.

however if you add the main menu to rightside bar it displays there but mine $main_menu is empty :(

Spent days on this I might just hard code the menu as its cost too much time. :(

jswainst’s picture

I ended up solving this issue by calling menu_tree and using the render function to output the html.

debrajn’s picture

Yes, I also solved the problem for main-menu using below code in page.tpl.php:-

$pid = variable_get('menu_main_links_source', 'main-menu');
$tree = drupal_render(menu_tree($pid));
print $tree;

You can also change your class of the menu.

Thanks