Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

How do I display all nested links in <ul><li> format in Drupal 7? The default code only displays the 1st level links even though they are arranged in the Drupal admin menu as nested:

<?php if ($main_menu): ?>
<div id="mainmenu"> <?php print theme( 'links__system_main_menu', array('links' => $main_menu) ); ?> </div>
<?php endif; ?>

Only the 1st level is displayed in the ff. menu setup:

-Fruits
---Apples
---Oranges
---Lemons
-Cars
---Big car
---Small car
---Fucked up car
-Whatever
share|improve this question
add comment

2 Answers 2

up vote 20 down vote accepted

The default $main_menu variable in page.tpl.php is set to menu_main_menu(), which is a contextual view of the main menu tree.

To get your desired output, you need to get the entire menu tree using menu_tree_all_data() and menu_tree_output().

To do this in your theme, create template.php if you haven't already, and add a new preprocess function for the page template:

function THEMENAME_preprocess_page(&$variables) {
  // Get the entire main menu tree
  $main_menu_tree = menu_tree_all_data('main-menu');

  // Add the rendered output to the $main_menu_expanded variable
  $variables['main_menu_expanded'] = menu_tree_output($main_menu_tree);
}

Then, in page.tpl.php, you can just use:

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

to get a expanded, nested unordered list of the main menu.

For a little more flexibility, you might want to consider the Menu Block module, which will let you create blocks of expanded menus out of the box with its Expand all children of this tree option as well as a host of other options for segmenting menu trees.

share|improve this answer
    
The print, at least in D7, only works this way: <?php print $main_menu_expanded; ?> Anyway, thanks a lot for the tip. –  user1644 Jun 15 '11 at 18:47
2  
Actually, you have to use print render($main_menu_expanded) on D7. –  Fidelix Jun 15 '11 at 19:05
    
@Eduardo Yeah, Fidelix is correct: you should have to use render() at some point. Otherwise you just get an array. –  user7 Jun 15 '11 at 19:48
    
The above code has a mistake, it should have the " & " infront of the variable declaration or nothing you do is saved for the page.tpl! function THEMENAME_preprocess_page(&$variables) { // Get the entire main menu tree $main_menu_tree = menu_tree_all_data('main-menu'); // Add the rendered output to the $main_menu_expanded variable $variables['main_menu_expanded'] = menu_tree_output($main_menu_tree); } –  Seb datarazor Sep 2 '11 at 7:42
1  
Fixed in the above answer. Note that you can edit all answers (or suggest an edit if you don't have enough points yet). –  Berdir Dec 7 '11 at 14:32
show 3 more comments

Also there is a menu_block module which allows creating arbitrary amount of customized blocks showing needed portions of menu

share|improve this answer
add comment

protected by kiamlaluno Nov 8 '12 at 14:37

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.