1

How do I change the default "active-trail" class on the navigation menu <li>-item to something different, or add an extra class to the current active item?

2

1 Answer 1

2

You can use the theme_menu_link function to add or remove classes from menu items. This will target all menu's on the page though.

Probably easiest is to target the menu directly with this function theme_menu_link__main_menu. You have to put display your menu as a block though. For bartik there is already a block available.

Steps

  • Uncheck Main menu at admin/appearance/settings/bartik
  • Put main menu block in a region (e.g. header region)
  • Now you can do:

    function bartik_menu_link__main_menu (&$variables) {
      $link = $variables['element'];
       if (isset($link)) {
        if (isset($link['#attributes'])) {
         $link_classes = $link['#attributes']['class'];
         if (in_array('active-trail', $link_classes)) {
          $variables['element']['#attributes']['class'][] = 'myclass';
         }
        }
       }
     return theme_menu_link($variables);
    }
    

If you want to style the main menu when it's not in a block you could use:

function bartik_links__system_main_menu($variables) {
  $links = $variables['links'];
  if ($links) {
    foreach ($links as $link) {
      $link_classes = $link['attributes']['class'];
      if (in_array('active-trail', $link_classes)) {
        // add or remove classes here
      }
    }
  }
  // return themed links
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.