Site logo

Archived topic

Links to #id of same page in Off Canvas Panel are all shown as if "current"

7 replies · Started by Jessica on June 14, 2022

Viewing posts 1–8 of 8

Our site is just a very simple one page with various sections (e.g. #quienes-somos, #que-hacemos, etc.) but still, we'd like to have off-canvas menu where people may click to go directly to a certain section on the same page.

I have created the menu where the URL to each menu item is: /#section-name (e.g. /#quienes-somos, /#que-hacemos, etc.) because without the slash at the beginning, the links breaks when I tried to use the menu from the News section (which are blog posts).
One example of our off canvas menus section URL

But with the links set as such in the menu, ALL of the sections are then considered as if they are all "current"! (In Appearance > Customize > Colors > Off Canvas Panel, the three options are "initial", "hover" and "current")

What I would like to archieve is to be able to navigate through these different items on the menu though that are just different sections on the same page, as if how it would be so if each section were a separate page altogether, like follows:
Effect that I would like to archiev, NOT with pages but with links to a section ID

Any insight on how may I set the menu item to be "current" only when the URL really has the section ID?
e.g. When I am at http://domain.com, the menu item "Inicio" would be the only "current" item but all other items maybe navigated through
When I am at http://domain.com/#quienes-somos, just the menu item "¿Quiénes somos?" is "current" item but all other items maybe navigated through.

Thanks a lot for your help!

Thank you for adding the images.

Adding a / automatically adds the current-menu-item class to your anchor tags. May I know what occurs when the / is removed? Can you try removing them and I’ll check from my end what’s occurring? Can you also share the link to your site?

If you remove the /, one thing we can do is add the current-menu-item class manually through JS.

For instance, we can add class anchor-link to your menu items as such: https://share.getcloudapp.com/9ZuoXYd6

Then, create a Hook Element hooked to wp_footer with this code:

<script>
var anchorLinks = document.querySelectorAll( '.anchor-link' );
const anclen = anchorLinks.length;
for ( var i = 0; i < anclen; i++ ) {
    anchorLinks[ i ].addEventListener( 'click', function() {
				for ( var b = 0; b < anclen; b++ ) {
					anchorLinks[ b ].classList.remove('current-menu-item');
				}
        this.classList.add('current-menu-item');
    } );
}
</script>

Here it is working from my end: https://share.getcloudapp.com/qGuONgrB

Kindly let us know.

Here is the screenshot of removing the slash before the #id from"¿Quiénes somos?" and "¿Qué hacemos?"
Screenshot after removed slash
The navigation in the menu works BUT it breaks when I want to use the menu from the Noticias section..

Because the noticias section URL is something like this:
https://domain.com/?page_id=116

And if I were to use the menu while I'm in the Noticias page, it breaks because it tries to reach this URL that doesn't exists..
https://domain.com/?page_id=116#quienes-somos

Thanks for pointing out that "Adding a / automatically adds the current-menu-item class to your anchor tags."
The fact that I can't remove the back slash in my menu links because the link breaks without it if I were to use this menu in another section (e.g. "Noticias" or blog posts).

Thanks for your instructions. Did almost everything as you said except that I have changed the javascript code a little bit to make it works.

1. Added "anchor-link" class to all my menu items starting with a back slash (e.g. /#quienes-somos, /#que-hacemos, etc)
2. Added a hook to be in "wp-footer" with the following script, which just simply remove "current-menu-item" from its class list.


<script>	
var anchorLinks = document.querySelectorAll('.anchor-link');
const anclen = anchorLinks.length;

for ( var i = 0; i < anclen; i++ ) {
	anchorLinks[ i ].classList.remove('current-menu-item');	
};
</script>

Thanks so much for your help!

I see. Glad you got it to work!

You’re welcome Jessica!

This archived topic is closed to new replies.