Classes per level of menu items links
Description
These are a series of example uses for having classes on your <a> tags of your menus, depending on the depth (or level) that they are inside the menu. All these would go in the template.php file of your theme as usual. For background information, please inform yourself on the API for theme_menu_item_link, the l() function, and Overriding themable output.
Note that these are for all other menus that are not primary/secondary links.
Classes per level of the menu link
This would result in something like <a class="level-1" href="/user/1">My account</a>:
<?php
function phptemplate_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
$link['localized_options']['attributes']['class'] .= ' level-' . $link['depth'];
return l($link['title'], $link['href'], $link['localized_options']);
}
?>Add a class only to a specific level of your menu links
This would result in something like <a class="first-level" href="/user/1">My account</a>:
<?php
function phptemplate_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
if ($link['depth'] == 1) { // 1 for first level, 2 for second, etc
$link['localized_options']['attributes']['class'] .= ' first-level'; // change first-level to whatever you'd like to be your class, don't forget the space before.
}
return l($link['title'], $link['href'], $link['localized_options']);
}
?>Use level classes only for a specific menu
This would result in something like <a class="level-1" href="/myurl">My menu level 1 link</a>:
<?php
function phptemplate_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
if ($link['menu_name'] == 'my-menu-name') { // change my-menu-name for the name of the menu you want the classes in
$link['localized_options']['attributes']['class'] .= ' level-' . $link['depth'];
}
return l($link['title'], $link['href'], $link['localized_options']);
}
?>Further customization
The way to go about it, if these examples don't fit your needs, is to do a dpm($links) (asuming you have the devel module installed) from within the phptemplate_menu_item_link function, see what info you have to work with, and then react to it. If you understand the code in the examples you should be able to get what you want pretty easily.
