I have created a menu in Drupal 7 and created links under that menu.

I named my new menu "Site Menu"

In my page.tpl.php where I want my menu to appear I have put this in place:

<?php print theme('links', menu_navigation_links('menu-site-menu')); ?>

After I have cleared my cache and refreshed my page my menu doesn't appear.

I am stumped.

Any help would be greatly appreciated.

Comments

demiurgus’s picture

I have exactly the same problem. I have copied the code that is supposed to print out the main-menu from system/page.tpl.php exactly:
<?php print theme('links__system_main_menu', array('links' => $main_menu, 'attributes' => array('id' => 'main-menu', 'class' => array('links', 'inline', 'clearfix')), 'heading' => t('Main menu'))); ?>

But nothing shows up. The menu does work with Stark theme.

I'm creating a custom design for a specific website, so I don't want to put the menu in a block. It will always be in the same place and behave the same way.

demiurgus’s picture

The problem for me was that I haven't enabled 'main_menu' as a feature in the .info file. My impression from the documentation was that this was only necessary if I wanted to dynamically place the menu in a block, but apparently not.

Stef Van Looveren’s picture

Wow, you saved me hours of looking for a solution! Thanks.

EricMcWinNer’s picture

Hello, I'm really new to Drupal and I created a main menu with this code, but I keep getting a Main Menu header above my navigation, I could get rid of it with css, but i don't want it to be displayed in the first place. I've tried searching for a block to get rid of it's title, but I don't find any block representing my main navigation. I know it's a relatively easy problem, so could anyone please help me out?

mellenger’s picture

If I create another menu, what's the code to print that menu out. For example, a "global tools" menu at the top or bottom of the page. All of the examples i have found online use the same "main menu."

I'm just using blocks right now to do it, like i did in D6, just wondering if there is a way to do it in the template.

thanks!

DartDev’s picture

So how exactly to print custom menu in drupal 7, it's so complicated or what?

DartDev’s picture

I found out myself how to print it, here we go:

$menu = menu_navigation_links('menu-your-custom-menu-name');
print theme('links__menu_your_custom_menu_name', array('links' => $menu));

Example:

$menu = menu_navigation_links('menu-main-page');
print theme('links__menu_main_page', array('links' => $menu));
damien_vancouver’s picture

The approach above worked great for me!

Here is an another working example.

I want to have a different menu appear in place of the secondary menu if the user is logged out (so I can provide extra login links to intranet sites, and a link to the Contact form in place of the default logged out Secondary menu which just contains "Sign In / Register").

So, I've created another menu with the links I want and named it: secondary-menu-logged-out.

Here is the code from my page.tpl.php that displays this custom menu when a user is logged out, in the same style and with the same (hidden) title as the secondary menu. I'm using a Zen subtheme.

if ($logged_in) { // below is the normal code to print the secondary menu from page.tpl.php:
	print theme('links__system_secondary_menu', array(
	      'links' => $secondary_menu,
	      'attributes' => array(
	        'id' => 'secondary-menu',
	        'class' => array('links', 'clearfix'),
	      ),
	      'heading' => array(
	        'text' => $secondary_menu_heading,
	        'level' => 'h2',
	        'class' => array('element-invisible'),
	      ),
	    )); 
    } else { // if logged out, print menu-secondary-menu-logged-out instead of the usual secondary menu!
      $menu = menu_navigation_links('menu-secondary-menu-logged-out');
      print theme('links__menu_secondary_menu_logged_out', array(
           	'links' => $menu,
        	'attributes' => array(
	        	'id' => 'secondary-menu',
	        	'class' => array('links', 'clearfix'),
	          ),
	      	'heading' => array(
	        	'text' => $secondary_menu_heading,
	        	'level' => 'h2',
	        	'class' => array('element-invisible'),	          
	          ),	      
      ));
   }

The first argument you pass to theme should be: 'links__' followed by the name of your menu with the hyphens converted to underscores. This defines the theme hook for your menu. My menu was named menu-secondary-menu-logged-out, so my argument to theme() is: 'links__menu_secondary_menu_logged_out'.

Because of this, I could override the theming for menu-secondary-menu-logged-out by creating a function named mythemename_menu_secondary_menu_logged_out() and theme it like any old item.

So you should set this first argument properly, otherwise you may find your printed menu inheriting the CSS styles of whatever menu is written in there. See the API page for Drupal 7 theme() for more info.

Yuvaraj_G’s picture

but it doesn't print the sub menu could you help me out please......

datarazor’s picture

all looks good but remember, logic should be in your template.php NOT in any page.tpl.php. A good page.tpl.php will only print variables that the template.php defined. Use template.php to preprocess the page and set the correct variable into the menu variable, then print it in page.tpl.php.

Kindly, datarazor.

Frederic wbase’s picture

I use the following method:

In template.php in the preprocess page function i put the follow code

 	//get all menu's
	$menus = array();
	$menus = menu_get_menus($all = TRUE);
	
	//create var for every menu so we can use it in our page.tpl.php
	foreach($menus as $key => $value){
		$var = str_replace('-','_',$key);
		$vars[$var] = menu_tree($key);
	} 

The you create variables that are available in page.tpl.php for every menu. To know the menu name go to admin/structure/menu & hover over the menu.

grts

frederic

http://www.wbase.be twitter: @wbase_be

Ed Francavilla’s picture

Just the top level menu
drupal_render(menu_tree('my-custom-menu'));

All nested menus

drupal_render(menu_tree_output(menu_tree_all_data('my-custom-menu')));

nocean’s picture

Great solution. Just a reminder (to others) to PRINT that if you're putting it in your template files. So it would be

print drupal_render(menu_tree('my-custom-menu'));

or

print drupal_render(menu_tree_output(menu_tree_all_data('my-custom-menu')));

Tharick’s picture

Its working cool :)

KirisuteRanza’s picture

This works fine, and allows for the possibility to create drop down menus as it outs puts all of it, but how do you get this to be like theme and out put as an array so that you can apply you own CSS classes.

At the moment I have for one custom menu; but I want my main menu to be outputted like this but all:

<?php $custom_menu = menu_navigation_links('menu-header-menu');?>
	<?php if ($custom_menu): ?>

	<?php print theme('links__system_menu_header_menu', array(
					'links' => $custom_menu,
					'attributes' => array(
				        'class' => array('no-list', 'inline-list', 'align-right', 'sub-nav'),
					),
					)); ?>
<?php endif; ?>

With the main menu I have:

<?php $main_menu_tree = menu_tree_all_data('main-menu'); ?>
<?php $main_menu_expanded = menu_tree_output($main_menu_tree);?>

<?php print render($main_menu_expanded);?>

But if I attempt to put it in the format like the custom menu it doesn't appear. Does anyone know if you can do this and if so how it is done?

stomerfull’s picture

Hello every body ,

I tried it and it works
here is mys code :

$menu = menu_navigation_links('main-menu');
print theme('links__system_main_menu', array('links' => $menu));

but for a multilingual site it does not work: the menus of all languages ​​are displayed
Example if I have a "Accueil" page in FR and "Home" in English and "Benvenuto" in Italian

the three menus are all displayed while I'm on the French interface, normally I should see the display menu "Accueil" only in french interface but not all
thank you very much for your help

stomerfull’s picture

when I do it like this to display my menu block I have the same problem

$blockt = module_invoke('menu', 'block_view', 'menu-conviction-menu');
print render($blockt['content']);

all my menus are displayed

or if I enable the block in drupal Back office to be displayed on a region of the page it works i have only the menu in French displayed on the French interface (Accueil)

stomerfull’s picture

My problem is the same that this post http://drupal.org/node/923994 but i'm in D7

stomerfull’s picture

Found solution

i m using this function :
i18n_menu_navigation_links (http://api.drupalize.me/api/drupal/function/i18n_menu_navigation_links/7)

and its work like a charm

hop it helps someone

thanks

Anonymous’s picture

Is someone willing to make a definitive writeup of this? If you want to sit down and work together on this just let me know, and I'll help where I can.

I see so many parts of code snippets, and comments that it worked as well as comments that it didn't work, that I can't make heads or tails out of how it is supposed to be done.

Please if you post something try to be as clear and complete as possible so that people can understand, or maybe it is just me?

From what I gather:
Were supposed to do this in template.php, and only call the output result in the template file (but which variables, how and why).
The theme function got replaced by the render function.

I've opened up a book page to build a documentation page on this, if you know how to do this the proper way please write it down so we can all save some time on this in the future. The book page can be found at How To Do Theming Examples Made Simple in Drupal 7. You can then add a child page for the subject.

Thank you for your participation! :-)

Edit:
Started a support issue How to display a menu in a template file?
If I may make the suggestion we consilidate efforts there on working on the issue before adding it to the final draft of the doc page.

sandipan4uonly’s picture

I was tring to print a custom menu which is created by menu through D7 menu panel , but can't print it out through the tpl file.
But I have solved this problem.

I have created a menu -- ( Menu name --> Home page right sidebar menu) but drupal create a system generated menu for this (In my case URL path: menu-home-page-right-sidebar-men)
Usage :

$menu = menu_navigation_links('menu-home-page-right-sidebar-men');
print theme('links__menu_home_page_right_sidebar_men', array('links' => $menu));
steveOR’s picture

I wanted to render just the first 2 levels of a large deep menu, and the menu_tree_all_data function works nicely! So as frogy130 and daobydesign already suggested here, just add depth parameter:

<?php
menu_depth = 2;
print drupal_render(menu_tree_output(menu_tree_all_data('my-custom-menu', null, menu_depth)));
?>
muthuraja143’s picture

In my development site i need to put the all main menu,sub munu in sitemap page

anyone please help me..

omrmankar’s picture

//Render the main menu In your template and block using below code

<?php
    $main_menu_tree = menu_tree(variable_get('menu_main_links_source', 'main-menu')); 
    print drupal_render($main_menu_tree);

?>



//Render user menu In your template and block using below code

<?php  
    $main_menu_tree1 = menu_tree(variable_get('menu_main_links_source', 'user-menu')); 
    print_r(drupal_render($main_menu_tree1));
 ?>

Best regards,

Omprakash Mankar
Senior Drupal Developer