1

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).

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.