Sunday, June 5, 2011

Add unique class (mlid) to all Drupal 7 menu items

Main and secondary menus has a great class .menu-123 (.menu-mlid) to do that. So here how you can port the same behavior to any/all menu items. Add the following function in your template.php file,

<?php
/**
* Add unique class (mlid) to all menu items.
*/
function YOURTHEMENAME_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";
}
?>

7 – 8 theme_menu_link(array $variables)

See core function theme_menu_link for Drupal 7 - 8 at http://api.drupal.org/api/drupal/includes--menu.inc/function/theme_menu_link/7

Thanks betarobot for this comment

1 comment:

Anonymous said...

I visited many ωеbsites excеpt the аudio feаturе for audio songѕ ρresent
at thіs site iѕ truly excellent.

Alsο vіsit my blоg pοst - raspberry ketone uk