Creating the <li> class=first and class=last on custom menus
When creating a menu, sometimes you want to have a class designation in the elements. A common use of this is to be able to create visual dividers between menu items, while making sure that the last item doesn't have a divider. As of Drupal 5, the engine will auto-generate some first and last class designations in your li items:
<li class="first">
<li>
<li>
<li class="last">
However, this only works for primary and secondary menu items. If you create a custom menu and you want this to happen, you'll need to put some code into your template.php file to get Drupal to create these class names. Just put this at the end of your theme's template.php file, and make sure to rename the function to the name of your theme (i.e. function themeName_menu_links):
//below code creates the ability to have last/first
//class designations on menu items, overriding the default.
/**
* Override theme_menu_links()
* - Add "first" and "last" class to first and last menu items
*/
function themeName_menu_links($links) {
if (!count($links)) {
return '';
}
$level_tmp = explode('-', key($links));
$level = $level_tmp[0];
$output = "<ul class=\"links-$level\">\n";
$num_links = count($links);
$i = 1;
foreach ($links as $index => $link) {
$classes = '';
if (stristr($index, 'active')) {
$classes .= "active ";
}
if ($i == 1) {
$classes .= "first ";
}
if ($i == $num_links) {
$classes .= "last ";
}
$output .= '<li class="' . $classes . '"';
$output .= ">". l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']) ."</li>\n";
$i++;
}
$output .= '</ul>';
return $output;
}