Archived topic
Current color of a button.
3 replies · Started by Jusung on March 13, 2023
Hello. this is about generate block, but I got no solution there.
I want to make a current color on button according to url.
Currently, there is no feature for this.
However, I thought there could be a way to change the color.
add_action( 'init', 'if_url_contains_string' );
function if_url_contains_string() {
$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( false !== strpos( $url, '/shop' ) ) {
echo '<style type="text/css">
.gb-container-ffa8ce99 { background-color: red }
</style>';
} else (false !== strpos( $url, '/product-category/모자') ) {
echo '<style type="text/css">
.gb-container-652ad9ca { background-color: red }
</style>';
} else (false !== strpos( $url, 'product-category/상의') ) {
echo '<style type="text/css">
.gb-container-5cdc6557 { background-color: red }
</style>';
} else (false !== strpos( $url, '/product-category/신발') ) {
echo '<style type="text/css">
.gb-container-64ee13dc { background-color: red }
</style>';
}
}
Unfortunately this doesn't work..
If this is something complicated and out of scope of support, it is ok not to answer!
I can try to figure out slowly alone.
Hi there,
in the Customizer > Colors, add a new Color, and give it a name eg. dynamic-color
this will register a CSS Variable eg. --dynamic-color: #00ff00;
apply that color to your button.
Then you can write some CSS to change that color based on a <body> class
eg. post-type-archive is the body_class for the shop page.
.post-type-archive {
-dynamic-color: #ff0000;
}
for a category archive page, you can use its term-{ID} that is on the body_class eg.
.term-23 {
-dynamic-color: #ff0000;
}
Just make as many term-{ID} rules as you need.
Thank you!!
that really works!
Glad to he of help