function scripts() {
echo <<<HTML
<script>
const currentNavLink = document.querySelector('.main-navigation a[href="' + window.location.pathname + '"]');
currentNavLink.style.backgroundColor = "red";
</script>
HTML;
// do not indent line above
return false;
} // END /**/
Keep in mind that Kookaburra doesn’t have jQuery, so your code above wouldn’t work at all. On top of that, the markup is different. So, to write JS for Kookaburra, you would need to:
use vanilla JS, not jQuery
inspect the page to get the appropriate elements, class names, etc.
In short, you cannot copy-and-paste Pangolin code into Kookaburra.
brilliant! Thank you!
I tried to address .main h2 in addition to .main-navigation but doesnt work.
I also tried to duplicate the script - one for the .main navigation the other one for .main h2 doesnt work either.
Is there a way to address .main h2 and .main navigation at the same time?
I didn’t understand what you were trying to do. I thought you just wanted to highlight the page’s heading.
I think you need a Javascript solution, something I really don’t know about.
Is this code that you’re manually putting on every tour page? If so, you could simply use inline styling.
If you’re adding it via an album template or phplugins then my guess is that you would need to use Javascript to identify the current page’s link in that group and add the styling like Matt showed with the nav items.
You’ll probably need to put your code inside a div with an ID and target the current page link within that div like Matt targets links inside .main-navigation a
Your photo tours menu points to the Barcelona page. Let’s say you’re Valencia page, the selector query would not match since the Valencia page link is different to the Barcelona link.
On my site, I use php to match pages and then add a class accordingly. Then it’s just some css to get the effect. But this can be done with plain javascript too. This should work, hopefully…
<script>
if (url.pathname.match(/photo-tours/)){
// highlight menu
document.querySelector('.main-navigation a[href="/barcelona-photo-tours"]').style.backgroundColor = "red";
}
</script>
In the javascript console you see the issue: Uncaught SyntaxError: Identifier 'currentNavLink' has already been declared (at barcelona-photo-tours:1901:13)
You have to use a different name.
My understanding is that this will work for the page https://oliver-blum.com/barcelona-photo-tours, but not for the other photo tour pages. My code should work for all.
you’ll excuse me, but Javascript is a closed book for me.
Unfortunately, your script only works partially. What do I have to change so that every active link in the main navigation is highlighted in color and the active main h2 links are addressed separately?
But it would be perfect, to find a solution, that all elements in the NavMenu (Home, About, FAQ, Contact) will also be highlighted with a background color. Therefore I tried to add:
if (location.pathname.match(/faq/)){
// highlight menu Faq
document.querySelector('.main-navigation a[href="/faq"]').style.backgroundColor = "rgb(255, 128, 0)";
}
to your script but without any effect
Do you have any other idea?
Best regards,
Oliver
BTW, the workshops are roughly described in german lang for now, will work on it today