Moving Menu Link ID to LI Tag in Drupal 6
If there is a shortcoming in Drupal, its the way the menu system is handled. In this particular case, its that custom ID's for links added via the Menu Attributes module are added to the anchor tag and not the list item tag. While this is great for theming that particular link, it can cause difficulties when using most jQuery scripts to modify the display of your menus since the <li> tags won't have any identification.
It's pretty simple to fix this by overriding theme_menu_item() in your theme's template.php file using the code below. Of course, replace "yourtheme" with your theme's name.
function <strong>yourtheme</strong>_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {<br> // Find the ID set on the <a> tag<br> if (preg_match("/id="([^"]+)"/", $link, $matches)) {<br> // Set the ID for the <li> tag<br> $id = $matches[0];<br> // Remove the ID from the <a> tag<br> $link = str_ireplace($matches[0], "", $link);<br> }<br> // Construct the navigation links - from theme_menu_item()<br> $class .= ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));<br> if (!empty($extra_class)) {<br> $class .= ' '. $extra_class;<br> }<br> if ($in_active_trail) {<br> $class .= ' active-trail';<br> }<br> return '<li class="'. $class .'"' . $id . '>'. $link . $menu ."</li>\n";<br> }
Of course, preg_match can be adjusted to move the class, title, or other attributes from the <a> tag if necessary. Personally, I'm surprised I hadn't run into a need for this sooner, but generally the default menu display or using the Superfish module has been good enough for me. However, this really came in handy for making the tabbed navigation work on the Cordell Package Store website.