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 BoyajyanJul 29, 2014 at 16:13
2 Answers
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()
-
-
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. Jul 29, 2014 at 17:24
-
UPDATE: if menu machine name have hyphne like main-menu then change hyphen to underscore, like MYTHEME_menu_tree__main_menu(). Jul 29, 2014 at 17:33
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.