The following will add a unique CSS id to all Drupal 5 menu items of the form #menu_item-123, where "123" is the $mid of the menu item in question.
It will also add a CSS class of the form .menu_item-menu_item_title, where "menu_item_title" is the title (i.e., link text) of the menu item, in lowercase letters, with all special characters converted to underscores.
Add the following to template.php:
* Add unique id's to menu items.
* Taken from http://www.nicklewis.org/node/843
* Requires menu_item.tpl.php
*/
// Override theme_menu_item() (includes/menu.inc)
function phptemplate_menu_item($mid, $children = '', $leaf = TRUE) {
$arr = array('leaf' => $leaf,
'mid' => $mid,
'children' => $children);
return _phptemplate_callback('menu_item', $arr);
}
Create and the following to menu_item.tpl.php in your current theme directory:
/**
* This template adds unique css ids to all menu items and css class
* names based on the menu item's title.
*/
$link = menu_item_link($mid);
// Preserve alphanumerics, everything else becomes a separator
$class = strtolower($class);
$class = strtolower(strip_tags(html_entity_decode($link) ));
$pattern = '/[^a-zA-Z0-9]+/ ';
$separator = '_';
$class = preg_replace($pattern, $separator, $class);
// render the menu link with unique CSS id.
$output = '<li id="menu_item-' . $mid . '" class="menu_item-'. $class . ' '
. ($leaf ? 'leaf' : ($children ? 'expanded' : 'collapsed'))
.'">'. $link . $children ."</li>\n";
print $output;
