Add a Class Selector to Menu Items

Sometimes you need a class selector in order to more easily target some particular item, such as a menu link.

Drop the following into the template.php file for your theme, clear your caches and all your menu items will have classes like "menu-123".

/**
* Add unique class (mlid) to all menu items.
*/
function YOUR-THEME-NAME_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';

$element['#attributes']['class'][] = 'menu-' . $element['#original_link']['mlid'];

if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

Contact Me

Feel free to contact me.

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.