I searched for something like single_cat_slug but I did not find something relevant. All I don’t know how to do it is to find the slug, not the name, of the currently opened category page. I do not know how to find the slug if I have the name.
<?php
$c = is_category();
$d = !empty(get_the_category());
$cat = $c ? single_cat_title('', false) :
( $d ? get_the_category()[0]->slug : NULL );
if ($cat !== NULL) {
?>
and after this there is some HTML in which I will use $cat to highlight the currently opened category (either a post in that category or a category archive page itself).
The issue in my case is that single_cat_title does not return the slug but the title (the name).
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
On a category archive page, you can use get_queried_object() to get the data of the current category in the main request, and get the slug like so: get_queried_object()->slug.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0