1

I'd like to change the class from "menu" on one specific menu I have showing up as a block on about 3 pages in a Drupal 7 site. I've been researching but can't seem to figure out the best way to go about doing this.

  • If it's only for 3 pages, I believe fastest/easiest way to do this is through JS. – Aram Boyajyan Jul 29 '14 at 16:13
3

You can do this with theme_menu_tree() hook in your theme's template.php file. Or with a more specific version of theme_mene_tree() hook like MYTHEME_menu_tree__MYMENU().

function MYTHEME_menu_tree__MYMENU(&$variables) {
    return '<ul class="nav newclass">' . $variables['tree'] . '</ul>';
}

See more here https://gist.github.com/gagarine/3201854

UPDATE:

if menu machine name have hyphne like main-menu then change hyphen to underscore, like MYTHEME_menu_tree__main_menu()

  • Would the MYMENU part just be the machine name of the menu? – gussbus Jul 29 '14 at 17:00
  • Yes MYMENU part is just machine name of the menu. I tried with "management" menu like MYTHEME_menu_tree__management it worked. But this will not work if menu machine name have hyphen like main-menu or user-menu. – MutantMahesh Jul 29 '14 at 17:24
  • UPDATE: if menu machine name have hyphne like main-menu then change hyphen to underscore, like MYTHEME_menu_tree__main_menu(). – MutantMahesh Jul 29 '14 at 17:33
0

If you need add class to your primary or secondary tabs you need change theme callback of theme_menu_local_tasks(). Use hook_registry_alter() to change callback. If you need change menu links, drupal has theme_links() which has $variables['attributes'] where you can set class for ul. For this solution you need create function mymodule_preprocess_links() and set $variables['attributes'] = array('class' => array('my-class')).

P.S. look at theme_links() ortheme_menu_local_tasks() function to implement some solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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