4.6.x menu.inc theme_menu_tree($pid = 1)
4.7.x menu.inc theme_menu_tree($pid = 1)
5.x menu.inc theme_menu_tree($pid = 1)
6.x menu.inc theme_menu_tree($tree)
7.x menu.inc theme_menu_tree($variables)

Returns HTML for a wrapper for a menu sub-tree.


$variables: An associative array containing:

  • tree: An HTML string containing the tree's items.

See also


Related topics


includes/menu.inc, line 1623
API for the Drupal menu system.


function theme_menu_tree($variables) {
  return '<ul class="menu">' . $variables['tree'] . '</ul>';


wgsimon’s picture

I don't see it documented elsewhere that you can implement a THEMENAME_menu_tree__MENU_NAME() function as an override for a specific menu.

See http://drupal.org/node/254940#theme-suggestions-for-menus.

plong0’s picture

Just a note when implementing this in a sub-theme, you may need to use the base theme's name in your function name.

I bumped into this when implementing a bootstrap sub-theme.
MyTheme_menu_tree worked
MyTheme_menu_tree_my_menu didn't work
bootstrap_menu_tree_my_menu worked

davewilly’s picture

Slight correction on this - you need two underscores before the menu name.

* Overrides theme_menu_tree().
* Add bootstrap 'navbar-nav' class to Top Navigation menu
function bootstrap_menu_tree__menu_top_navigation(&$variables) {
return '<ul class="menu nav navbar-nav">' . $variables['tree'] . '</ul>';

gagarine’s picture

This function doesn't provide any context information like level or block id. This is a problem when you display the same menu in different block.

But you can use hook_block_view_alter to change the theme_wrapper.

I made a small example here https://gist.github.com/3201854

natts’s picture

In case this is useful to anyone, I couldn't get it to work in html.tpl.php or page.tpl.php etc., but in template.php it worked fine.

phponwebsites’s picture

How to add custom class to nested ul tag in drupal7?
I want to get my code as
<ul class="menu">
<li><a href="#">First</a>
<ul class="sub-menu">
<li><a href="#">one</a></i>
<li><a href="#">two</a></i>

allaniftrue’s picture

How was the development? were you able to do it? Mind sharing how you did it?

Ahmed_Samir’s picture

W.M.’s picture

Any idea how to add, for example, the class "navbar-nav" only to the parent ul menu inside main_menu. The above mentioned code wraps all ul's (ie. including the children) with the same class.

geru’s picture

This is a well-known issue with theme_menu_tree()

There are several ways around it:

  • make a copy of the whole menu tree for each child (#6) -- excessive overhead
  • use hook_theme_registry_alter() to remove template_preprocess_menu_tree() from the set of preprocess functions (#7)
  • use a patch to menu.inc to add a conditional that looks for attributes and copies them to the children if they exist.

With the last option, you can do lots of interesting things like keep track of levels and have ID's on each level and reference parent ID's to customize the output in template_menu_tree_menu() depending on level and parents. Hopefully, this simple patch can make its way into core.

web506’s picture

How can I print the Title of a Menu Only?

pinueve’s picture

I case you are using a bootsrap sub-theme, use function bootstrap_xxx_... instead of YOURSUBTHEME_xxx_...

stefan.marin’s picture

theme_menu_tree__main_menu($variables) did not work.
function theme_menu_tree__main_menu($variables) {
return '

Does anybody have this issue also?