I am new to drupal theming.I want to add class only to top level ul in main menu.Using theme_menu__tree_main_menu() affect all the ul tag.I found this for adding class only to top level..https://www.drupal.org/node/988694 function THEMENAME_menu_tree__menu_top_menu($variables) { return '' . $variables['tree'] . ''; } But it didn't work.Doesn't adding any class.Am I missing something?Please Suggest Me!!Thank

1 Answer 1


That example is missing some code. Those two empty strings should not be empty.

There is a link on that page just above the code example to api documentation of theme_menu_tree() https://api.drupal.org/api/drupal/includes%21menu.inc/function/theme_menu_tree/7.x

On the api documentation page you can see it done correctly:

return '<ul class="menu">' . $variables['tree'] . '</ul>';
  • I updated that documentation page on the Drupal site for future viewers.
    – rjl
    May 20, 2017 at 23:11
  • Thank!!.I already try "<ul class='myclass'>" in theme_menu_tree__menu_top_menu() but still doesn't work.
    – Cheng
    May 21, 2017 at 2:15
  • Do you know in fact that your function is being called? Could be a issue with function name - if you are calling this from your theme, the function name should start with 'YOUR_THEME_NAME_menu_tree...' and not 'theme_menu_tree...' Did you Clear All Caches (under Admin->Config->Performance)?
    – rjl
    May 21, 2017 at 4:18
  • Thank!!Yes I clear all the cache.My theme name is "abc" so I called the function abc_menu_tree__menu_top_menu(&$variables).Does this function require to override working only for main menu.
    – Cheng
    May 21, 2017 at 4:36
  • Your function should only override the one menu named 'menu_top_menu' and not any other menus.
    – rjl
    May 21, 2017 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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