Adding an "active" class to primary links - Option 1

Because primary links use theme_links to generate their output, we could override that theme and add in our own code to get an "active" class on the <li> element. Drupal 6 made some improvements to 5's theme_links function, so I used that version and added in some code to check for the front page too.

The front page thing is a bit controversial - some people don't think the front page should get an active class. Heck, some people don't put a Home link in their primary menu, expecting all visitors to simply click the logo to go to the front page. Sadly, not all users are that savvy, so adding a Home link somewhere is usually a good idea, in my opinion.

But I digress...here's the code (you can change 'phptemplate' to your theme name, if you want to):

<?php
/**
* Duplicate of theme_links, but adds active class to <li> elements.
*
* Return a themed set of links.
*
* @param $links
*   A keyed array of links to be themed.
* @param $attributes
*   A keyed array of attributes
* @return
*   A string containing an unordered list of links.
*/
function phptemplate_links($links, $attributes = array('class' => 'links')) {
 
$output = '';

  if (count($links) > 0) {
   
$output = '<ul'. drupal_attributes($attributes) .'>';

    $num_links = count($links);
   
$i = 1;

    foreach ($links as $key => $link) {
     
$class = $key;

      // Add first, last and active classes to the list of links to help out themers.
     
if ($i == 1) {
       
$class .= ' first';
      }
      if (
$i == $num_links) {
       
$class .= ' last';
      }
     
// Added in a check for front page here.
     
if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && $_GET['q'] == 'node'))) {
       
$class .= ' active';
      }
     
$output .= '<li class="'. $class .'">';

      // Is the title HTML?
     
$html = isset($link['html']) && $link['html'];

      // Initialize fragment and query variables.
     
$link['query'] = isset($link['query']) ? $link['query'] : NULL;
     
$link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;

      if (isset($link['href'])) {
       
$output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
      }
      else if (
$link['title']) {
       
//Some links are actually not links, but we wrap these in <span> for adding title and class attributes
       
if (!$html) {
         
$link['title'] = check_plain($link['title']);
        }
       
$span_attributes = '';
        if (isset(
$link['attributes'])) {
         
$span_attributes = drupal_attributes($link['attributes']);
        }
       
$output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
      }

      $i++;
     
$output .= "</li>\n";
    }

    $output .= '</ul>';
  }

  return $output;
}
?>

Put this function (without the PHP tags) in your template.php file. If you don't have a template.php file in your theme folder, create one and add the opening PHP tag to the top of the file first.

The next method is not as clean as this one, IMO, but its here for completeness.

Posted in:

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <b> <i> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <h4> <blockquote> <img>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is to block spam bots and check if you're human
b
E
c
v
M
y
Enter the code without spaces and pay attention to upper/lower case.
magnanimous-junior