質問

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

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません wordpress.stackexchange
scroll top