Isolate a specific menu item with $mid

Last modified: August 26, 2009 - 23:52

This is an additional tweak to the HowTo: Have "first" and "last" classes on menu blocks code.

Sometimes I have the need to style just a single item in a menu (use a different color or background for that item specifically). By adding the $mid into the class array for each list item, I have complete control over this with CSS.

My modified function looks like this:

<?php
function phptemplate_menu_item($mid, $children = '', $leaf = TRUE, $extraclass = '') {
  return
'<li class="'. ($leaf ? 'leaf' : ($children ? 'expanded' : 'collapsed')) . ($extraclass ? ' ' . $extraclass : '') . ' mid-' . $mid . '">'. menu_item_link($mid, TRUE, $extraclass) . $children ."</li>\n";
}
?>

All I added is ". ' mid-' . $mid", and didn't bother with conditionals since $mid is always defined.

In the CSS I use a selector like #header .mid-123 to isolate the menu item as used in the header, etc. Works great.

 
 

Drupal is a registered trademark of Dries Buytaert.