Output from my theme_links($secondary_links, array('class' => 'links secondary-links')) function looks like this:

<div id="secondary"><ul class="links secondary-links"><li class="first menu-2-1-74"><a href="/taxonomy/term/5" class="menu-2-1-74 active">View Submitted News</a></li>
<li class="last menu-2-2-74"><a href="/node/add/news" class="menu-2-2-74">Add News</a></li>
</ul></div>

The A tags have the "active" class when applicable, but I'd like the LI tags to have that class as well so that I can style with CSS. I've tried a custom function, which seems like it should work, but isn't. Where is the "active" class coming from for the A tags, and how do I get it in the LI tags also? Thanks!!

function kevin_theme_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 = '';

      //MY CHANGES:
      //Search for 'active' in $key and append to $class for LI.
      if (strpos($key, '-active') !== FALSE) {
      $class = 'active ';
      }

      // END MY CHANGES 

      // Automatically add a class to each link and also to each LI
      if (isset($link['attributes']) && isset($link['attributes']['class'])) {
        $link['attributes']['class'] .= ' ' . $key;
        $class .= $key;
      }
      else {
        $link['attributes']['class'] = $key;
        $class .= $key;
      }

      // Add first and last classes to the list of links to help out themers.
      $extra_class = '';
      if ($i == 1) {
        $extra_class .= 'first ';
      }
      if ($i == $num_links) {
        $extra_class .= 'last ';
      }
      $output .= '<li class="'. $extra_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']);
        }
        $output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
      }

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

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

  return $output;
}

Comments

kevinbock’s picture

Sorry, I meant to say these are my changes:

      //MY CHANGES:
      //Search for 'active' in $key and append to $class for LI.
      if (strpos($key, 'active') !== FALSE) {
      $class = 'active ';
      }

      // END MY CHANGES 

Hope that makes sense...

kevinbock’s picture

Nevermind, found a way to get what I needed.

For those interested, the code is:

      if ($link['href'] == $_GET['q']) {
      $class .= ' active';
       }
HINGE’s picture

Kevin - I would love to be able to utilize an "active" class on the li element as well. What file do you paste this into?

kevinbock’s picture

This solution customizes a theme_links function to add an active class to the li of a menu link if that menu links to the webpage you are currently viewing. (If the link is to /node/4 and you're viewing /node/4, then the link's LI will gain the active class.) It does not affect any items that are in the link trail.

Copy the theme_links {...} function from theme.inc to template.php, rename it to something (say, hinge_links) and add my line of code before any $output .= lines.

In your page.tpl.php file, find the place where the theme_links function is called and replace it with hinge_links.