What is an equivalent of single_cat_title for getting the slug of the category?
-
26-04-2021 - |
Frage
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).
Lösung
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
.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange