sage icon indicating copy to clipboard operation
sage copied to clipboard

'Active' is being added to the 'wp_nav_menu' multiple times

Open alkymst opened this issue 14 years ago • 20 comments

I have a possible fix, all though not fully tested.


add_filter('nav_menu_css_class', 'remove_active_nav_menu_css_class');
function remove_active_nav_menu_css_class($classes) {
    if(!empty($classes)) {  
        unset($classes);
        $classes = array('active');
    }
    return $classes;
}


alkymst avatar Jun 25 '12 05:06 alkymst

Are you able to screenshot the issue ? appreciate you taking the time to report a possible bug but im having trouble understanding where this is happening.

Regards, Darren

expertpixels avatar Jun 25 '12 05:06 expertpixels

It's using the "Roots_Nav_Walker" and with a menu that is showing children, and you are on a second level page. It happens because of the "roots_wp_nav_menu" function. It's re-writes the WP defaults, but at that point it is dealing with strings, vs. an array.

The way I proposed was to get at it earlier when we are still dealing with arrays.

alkymst avatar Jun 25 '12 05:06 alkymst

Ahh ok Thanks, yes i see what you mean now. Thanks for posting you fix i'll have a play around later tonight. Cheers

expertpixels avatar Jun 25 '12 06:06 expertpixels

I can dive in deeper, if needed. It fixed my issue, but if we see some other bugs because of it, I'm more than willing to help come up with something a little more robust.

alkymst avatar Jun 25 '12 06:06 alkymst

If this is a real bug your solution posted seems like a hack rather than a proper fix. I'd like to avoid adding more (separate) code to fix the issue when there's probably a better way to fix it in the Nav Walker.

swalkinshaw avatar Jun 25 '12 15:06 swalkinshaw

I would agree, just figured I would post it if it helped someone out. I was thinking the walker was the better option as well. I'll see if I can find some time to fix it.

alkymst avatar Jun 25 '12 16:06 alkymst

Not sure if this is in the same ball park.. ut say i have a drop down menu.. then click a child link.. the tab with the dropdown is not highlighted as active.

expertpixels avatar Jun 25 '12 23:06 expertpixels

I've updated the nav walker in my fork. It checks the classes and will add the active class on a match. This means it retains the other classes, unlike the previous string replace.

The walker also adds support for the nav bar classes in bootstrap: divider, divider-vertical and nav-header. If these classes are used in a custom menu you will now get the expected behaviour.

The main changes are below. You'll need to delete the roots_wp_nav_menu function and filter.

  function start_el(&$output, $item, $depth, $args) {
    global $wp_query;
    $indent = ($depth) ? str_repeat("\t", $depth) : '';

    $slug = sanitize_title($item->title);
    $id = 'menu-' . $slug;

    $li_attributes = '';
    $class_names = $value = '';

    $classes = empty($item->classes) ? array() : (array) $item->classes;

    if ($args->has_children) {
      $classes[]      = 'dropdown';
      $li_attributes .= ' data-dropdown="dropdown"';
    }

    $classes = array_filter($classes, array(&$this, 'check_current'));

    if ($custom_classes = get_post_meta($item->ID, '_menu_item_classes', true)) {
      foreach ($custom_classes as $custom_class) {
        $classes[] = $custom_class;
      }
    }
    $current_classes = array(
    'current-menu-item',
    'current-menu-parent',
    'current-menu-ancestor',
    'current_page_item',
    'current_page_parent',
    'current_page_ancestor'
    );
    foreach ($current_classes as $current_class ) {
    if (in_array($current_class,$classes)) {
    $classes[]="active";
    break;
    }
    }

    $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
    $class_names = $class_names ? ' class="' . $id . ' ' . esc_attr($class_names) . '"' : ' class="' . $id . '"';
    if (in_array("divider",$classes)) { 
            $item_output = "\t" . '<li'. $class_names .'>';
    } elseif (in_array("divider-vertical",$classes)){
            $item_output = "\t" . '<li'. $class_names .'>';
    } elseif (in_array("nav-header",$classes)){
            $item_output = "\t" . '<li'. $class_names .'>'. $item->title .'';
    } else { 

    $output .= $indent . '<li' . $class_names . $li_attributes . '>';

    $attributes  = ! empty($item->attr_title) ? ' title="'  . esc_attr($item->attr_title) .'"'    : '';
    $attributes .= ! empty($item->target)     ? ' target="' . esc_attr($item->target    ) .'"'    : '';
    $attributes .= ! empty($item->xfn)        ? ' rel="'    . esc_attr($item->xfn       ) .'"'    : '';
    $attributes .= ! empty($item->url)        ? ' href="'   . esc_attr($item->url       ) .'"'    : '';
    $attributes .= ($args->has_children)      ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';

    $item_output  = $args->before;
    $item_output .= '<a'. $attributes .'>';
    $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
    $item_output .= ($args->has_children) ? ' <b class="caret"></b>' : '';
    $item_output .= '</a>';
    $item_output .= $args->after;
    }
    $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
  }

Foxaii avatar Jun 26 '12 03:06 Foxaii

I am having other problems with the nav walker, I have a custom post type 'sponsor' and if I visit a single post of that post type, the 'News' link is marked as active.

This should be restricted to the 'post' post type, and should not be active on just any post type.

FellowshipAgency avatar Jul 19 '12 07:07 FellowshipAgency

@Rubious that is not a roots issue. if you activate another theme/use the core menu walker you'll notice that current_page_parent is applied to the blog page in the menu.

this bug is almost fixed - roots_wp_nav_menu() needs to be updated to only return active once. like @swalkinshaw said, the code provided at the beginning of this issue is more of a hack.

retlehs avatar Jul 19 '12 15:07 retlehs

Maybe it would be easier to tweak roots_wp_nav_menu function?

Eg.

function roots_wp_nav_menu($text) {
  $text = preg_replace('/(current(-menu-|_page_)(item|parent|ancestor))/', 'active', $text);
  $text = preg_replace('/( active){2,}/', ' active', $text);
  return $text;
}

It assumes that current classes were rendered in a row, but I've never seen them separated by other classes, so it should be enough. And it works for me now.

kapooostin avatar Aug 01 '12 06:08 kapooostin

@kapooostin thanks for the code. Just need a small tweak to also catch current-page-* classes.

swalkinshaw avatar Aug 15 '12 18:08 swalkinshaw

@Foxaii it would be great to add support for divider classes. Now that this bug has been fixed, it would be good if you could open a Pull Request with just the divider changes.

swalkinshaw avatar Aug 15 '12 19:08 swalkinshaw

@swalkinshaw

/(current[-_](menu|page)[-_](item|parent|ancestor))/

Something like this?

But in my project I left out a parent class. It was necessary for custom menu with pages from different levels of hierarchy: I have All Posts, A Category and Another Category in one submenu.

kapooostin avatar Aug 15 '12 19:08 kapooostin

@kapooostin sorry worded that badly. I already made the tweak my commit earlier.

swalkinshaw avatar Aug 15 '12 19:08 swalkinshaw

Ok :)

kapooostin avatar Aug 15 '12 20:08 kapooostin

From what I've seen the accepted fix removes the classes it matches, meaning there is no way to distinguish between parents and ancestors etc. Was this intended? It may be cleaner but it prevents any CSS distinction between the two.

My proposed fix adds the active class for this very reason. It's also easier to add or remove a class that would lead to an "active" class, meaning a solution to issue #518 where the user wanted only one element to match active.

I've updated the walker to support the new submenu in Bootstrap 2.1 but wanted to revisit this before a pull request.

Foxaii avatar Aug 21 '12 20:08 Foxaii

I still have this issue in 7.0.2. My menu has a blog link and a category with categories as sub-menus. Both "blog" and "skills" gets 'active' when either one is selected. http://i.imgur.com/OxydCOa.png [edit] Actually, they only both get 'active' when a sub-menu under Skills is selected.

joelstransky avatar Jan 21 '15 07:01 joelstransky

@joelstransky see @Foxaii's nav rejig branch on this repo / ref #986

this is being addressed in 8.0.0

retlehs avatar Jan 21 '15 07:01 retlehs

awesome thanks.

joelstransky avatar Jan 21 '15 08:01 joelstransky