-
Creative Reload
What is the best way for me to add different colors to buttons to represent that the button links to the current page?
-
Alvind
Hi there,
Are you adding the button links manually by typing the URL for each button yourself, or would you like the site to automatically detect when a button links to the current page and change its color automatically?
The solution will be a bit different depending on which approach you need, so let us know!
-
Creative Reload
Hey, Alvind!
I would like the site to automatically detect when a button links to the current page and change its color automatically.
Is it possible to just give the button a class that would take the colors from the WordPress customizer for header navigation?
-
George
Hi there,
Just so we can point you in the right direction — could you share a link to the page where these buttons are set up?
If you’re using a group of buttons as a page navigation menu, it might be simpler to use a GenerateBlocks Pro navigation block and set the current state color. Alternatively, you could set up a secondary navigation menu (Secondary Nav module needs to be activated) and display your menu there. There are settings in the Customizer to style the secondary menu (once the Secondary Nav module is activated).
-
Creative Reload
Hey, George!
I’ve considered the navigation block but GeneratePress doesn’t have great support for icons within the navigation menu.
Also, I have no page to link. This build is on my local machine only at the moment.
-
George
Hi there,
That makes sense — thanks for clarifying!
Here’s a small JavaScript snippet that will automatically detect when a button links to the current page and add a class to it.
First, wrap your buttons to a GB container with the class of
button-nav.Then add this to a GP Hook Element set to display site-wide using the
wp_footerhook:<script> document.addEventListener('DOMContentLoaded', function() { var currentPath = window.location.pathname.replace(/\/+$/, '') || '/'; document.querySelectorAll('.button-nav a.gb-text').forEach(function(link) { if (link.href) { var linkPath = new URL(link.href, window.location.origin).pathname.replace(/\/+$/, '') || '/'; if (linkPath === currentPath) { link.classList.add('is-current-page'); } } }); }); </script>Then style the active button with CSS:
a.gb-text.is-current-page { background-color: #your-color; color: #fff; }The script compares each link’s URL to the current page URL, and when they match it adds the
is-current-pageclass. Just adjust the colors to suit your design.Let us know how that goes!
-
Creative Reload
This is great. However, it only works until I add the icon which changes the selectors for the button.
-
George
Hello,
As you haven’t provided a URL so that we can have a look, would you be able to inspect the button and adjust the selector in the code provided? If not, please provide a URL so that we can have a look.
-
Creative Reload
<div class="gb-element-bf05cae3 button-nav"> <a class="gb-text-a4f4f681" href="http://creative-reload.local/"><span class="gb-shape"><svg aria-hidden="true" role="img" height="1em" width="1em" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"><path fill="currentColor" d="M466.5 83.7l-192-80a48.15 48.15 0 0 0-36.9 0l-192 80C27.7 91.1 16 108.6 16 128c0 198.5 114.5 335.7 221.5 380.3 11.8 4.9 25.1 4.9 36.9 0C360.1 472.6 496 349.3 496 128c0-19.4-11.7-36.9-29.5-44.3zM256.1 446.3l-.1-381 175.9 73.3c-3.3 151.4-82.1 261.1-175.8 307.7z"></path></svg></span></a> <a class="gb-text gb-text-06dc7697" href="http://creative-reload.local/sample-page/">Sample Page</a> </div>Here are two different buttons. The top one with the gb-shape span class doesn’t react like I am wanting it to. The bottom one works just fine.
-
Creative Reload
<a class="gb-text-a4f4f681" href="http://creative-reload.local/"><span class="gb-shape"><svg aria-hidden="true" role="img" height="1em" width="1em" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"><path fill="currentColor" d="M466.5 83.7l-192-80a48.15 48.15 0 0 0-36.9 0l-192 80C27.7 91.1 16 108.6 16 128c0 198.5 114.5 335.7 221.5 380.3 11.8 4.9 25.1 4.9 36.9 0C360.1 472.6 496 349.3 496 128c0-19.4-11.7-36.9-29.5-44.3zM256.1 446.3l-.1-381 175.9 73.3c-3.3 151.4-82.1 261.1-175.8 307.7z"></path></svg></span><span class="gb-text">Home</span></a>Here it is with an icon and text. When adding an icon, it removes the “gb-text” class from the button itself and puts “gb-text” and “gb-shape” into their own spans within “gb-text-a4f4f681”
-
Creative Reload
I manually added the “gb-text” class back to the button and solved the issue!
-
George
Great, good to know!
- You must be logged in to reply to this topic.