Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple menu items are being highlighted as current page #18

Open
Seblon opened this issue Sep 7, 2021 · 18 comments
Open

Multiple menu items are being highlighted as current page #18

Seblon opened this issue Sep 7, 2021 · 18 comments

Comments

@Seblon
Copy link

Seblon commented Sep 7, 2021

First, I would like thank you for this amazing contribution! Without it I would be lost :)

Issue:

When the menu item for a custom post type archive (archive-karriar.php) is active, then also the blog menu item (home.php or index.php - which ever used) is hi-lighted as the current menu item.

Strange thing is, this does not happen the other way around - when home.php or index.php menu item is active, the cpt archive is not hi-lighted as current page. Other than that the walker i working flawlessly.

I'm using

  • Wordpres 5.8
  • Bootstrap 5.1

This is the code I'm currently using in header.php:

<nav id="main-nav" class="navbar navbar-expand-lg fixed-top navbar-light bg-light" aria-labelledby="main-nav-label">

  <div class="container-fluid">
    <a class="navbar-brand" href="<?php echo site_url(); ?>">
      <img src="<?php echo get_theme_file_uri('/img/logo.svg'); ?>" alt="" class="custom-logo">
    </a>

    <button class="hamburger hamburger--squeeze" type="button" data-bs-toggle="collapse" data-bs-target="#main-menu" aria-controls="main-menu" aria-expanded="false" aria-label="Toggle navigation">
      <span class="hamburger-box">
        <span class="hamburger-inner"></span>
      </span>
    </button>

    <div class="collapse navbar-collapse" id="main-menu">
      <?php
      wp_nav_menu(array(
        'theme_location' => 'main-menu',
        'container' => false,
        'menu_class' => '',
        'fallback_cb' => '__return_false',
        'items_wrap' => '<ul id="%1$s" class="navbar-nav ms-auto mb-2 mb-md-0 %2$s">%3$s</ul>',
        'depth' => 2,
        'walker' => new bootstrap_5_wp_nav_menu_walker()
      ));
      ?>
    </div>
  </div>
</nav>

I'm not sure what else info you would need, but if something is missing just let me know. Thnx!

@xrdm
Copy link

xrdm commented Dec 2, 2021

same problem here.

@crftwrk
Copy link

crftwrk commented Dec 2, 2021

@xrdm do you have a link to your site?

@xrdm
Copy link

xrdm commented Dec 2, 2021

yes it's on mainteinance but if you need I can make it visible for a few minutes

@crftwrk
Copy link

crftwrk commented Dec 2, 2021

yes please, a link will help.

@xrdm
Copy link

xrdm commented Dec 2, 2021

@crftwrk
https://www.legalmalpractice.it/

"Articoli" are standard wp posts
"Ultime sentenze" & "Convegni" are cpt (Convegni -> Prossimi o in corso give not problem because it's using a page tpl).

thank you

@xrdm
Copy link

xrdm commented Dec 2, 2021

this is the snippet for active class in my class-bootstrap-5-navwalker.php

$active_class = ($item->current || $item->current_item_ancestor || in_array("current_page_parent", $item->classes, true) || in_array("current-post-ancestor", $item->classes, true)) ? 'active' : ''; $nav_link_class = ( $depth > 0 ) ? 'dropdown-item ' : 'nav-link '; $attributes .= ( $args->walker->has_children ) ? ' class="'. $nav_link_class . $active_class . ' dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"' : ' class="'. $nav_link_class . $active_class . '"';

@crftwrk
Copy link

crftwrk commented Dec 2, 2021

Please switch v1.3.3 release https://github.com/AlexWebLab/bootstrap-5-wordpress-navbar-walker/releases/tag/v1.3.3, just for testing.

@xrdm
Copy link

xrdm commented Dec 2, 2021

yes it works
but not in single articles.
thank you

@crftwrk
Copy link

crftwrk commented Dec 2, 2021

It's the current_item_ancestor added in v1.3.4 to show active link in WooCommerce product category. Good to know ;-)

@xrdm
Copy link

xrdm commented Dec 2, 2021

It's the current_item_ancestor added in v1.3.4 to show active link in WooCommerce product category. Good to know ;-)

now with 1.3.4 it's again not correct maybe :)

@pierrerocket
Copy link

Im running into the same issue here. I have the latest version of the walker. When on a custom post type single the blog link A in the nav is getting the active class, not the custom post type archive.

When I am on the custom post type archive page both the blog and cpt archive get the active class in the nav.

@qooglobal
Copy link

I'm facing the same problem.

Actually, I have a kind of single-page website. All the navbar menu items scroll to a specific section of the page.

The problem is all links have the active class.

The website is available here: http://development.8x.com/

Does anyone have the solution?

Thanks

@crftwrk
Copy link

crftwrk commented Mar 31, 2022

That's because you use full URL in menu https://development.8x.com/#posts. Using full URL, scrollspy sets active class to menu-items. You must use short URL #posts for scrollspy.

  1. Activate scrollspy on frontpage only. Change your frontpage id in line 2:
function add_scrollspy() {
    if( is_page('2257') ) { // Page id
      echo "<script>
        jQuery(document).ready(function ($) {
            var scrollSpy = new bootstrap.ScrollSpy(document.body, {
                target: '#top-section',
                offset: 58
            })
        });
      </script>\n";
   }
}
add_action( 'wp_footer', 'add_scrollspy', 0 );
  1. Install https://wordpress.org/plugins/conditional-menus/ plugin. Now create two menus. One with short URLs to sections, one with long URLs. Set the menu with short URLs to frontpage and use menu with long URLs in all other pages. Check here https://bootscore.me/documentation/scrollspy-onepager/.

Does this help?

@qooglobal
Copy link

Thanks. It worked @crftwrk

@midaspider
Copy link

When I am on the custom post type archive page both the blog and cpt archive get the active class in the nav.

I have the same issue. When on a CPT archive the 'active' class is also added to the blog archive link.
When on a CPT single the active class is not applied to the correct menu or sub menu link but it is again applied to the blog archive link.

Seems the walker is not currently compatible with WordPress custom post types?

@sancri
Copy link

sancri commented Jun 12, 2023

The problem it's not in the walker code, the core wp_nav_menu function wrongfully adds a current_page_parent class to the menu item of the page assigned as the page_for_posts when a custom post type single or archive page is displayed.
https://core.trac.wordpress.org/ticket/38486
https://core.trac.wordpress.org/ticket/13543

@CiceronePavaloi
Copy link

The bootstrap-5-wordpress-navbar-walker does filter the items classes when building the $class_names but when it sets the $active_class it checks against $item->classes which is unfiltered... So if one tries to fix the 'current_page_parent' class bug with a filter, it can't work.

I suggest replacing the line

$class_names =  join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));

with

$item->classes = apply_filters('nav_menu_css_class', array_filter($classes), $item, $args);
$class_names = join(' ', $item->classes);

@shrinitech
Copy link

Please switch v1.3.3 release https://github.com/AlexWebLab/bootstrap-5-wordpress-navbar-walker/releases/tag/v1.3.3, just for testing.

Hi, I was getting the same multiple highlighting issue, switching to v1.3.3 fixed it, thanks 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants