Pedro Posada | Set menu trail in primary links; Add active class to parent primary link

Set menu trail in primary links; Add active class to parent primary link

Posted on November 3, 2010 - 10:41am
<< 3 of 54 >>

Insert code snippet in template.php file.

<?php
/*
* Override theme_links()
*/
function MYTHEME_links($links, $attributes = array('class' => 'links'))
{
   
/*
     * collect all links in breadcrumb
     */
   
$breadcrumb_links = array();
    foreach((array)
menu_get_active_trail() as $crumb)
    {
       
$breadcrumb_links[] = $crumb['href'];
    }
   
      global
$language;
     
$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';
          }
          if (isset(
$link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
               && (empty(
$link['language']) || $link['language']->language == $language->language)) {
           
$class .= ' active';
          }
         
         
/*
           * check to see if this link is in the breadcrumb
           * set class to active if so
           */
         
if(in_array($links[$key]['href'], $breadcrumb_links))
          {
             
$class .= ' active';
          }
         
         
$output .= '<li' . drupal_attributes(array('class' => $class)) . '>';
   
          if (isset(
$link['href'])) {
           
// Pass in $link as $options, they share the same keys.
           
$output .= l($link['title'], $link['href'], $link);
          }
          else if (!empty(
$link['title'])) {
           
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
           
if (empty($link['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;
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.