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:

---Big car
---Small car
---Fucked up car
  • Also there is a menu_block module which allows creating arbitrary amount of customized blocks showing needed portions of menu Nov 10, 2011 at 14:47

1 Answer 1


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.


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