Woocommerce get the Archive/Category slug of the current category page


I want to get the slug of the current archive page.
I have a slider where I want to echo the products that are in this category.
After hours this is all I have and it doesn’t work…


if ( is_single() ) {
    $cats =  get_the_category();
    $cat = $cats[0]; // let's just assume the post has one category
else { // category archives
    $cat = get_category( get_query_var( 'cat' ) );
$cat_name = $cat->name;
$cat_slug = $cat->slug;

echo '<h2 style="text-align:center;">'.$cat_name.' - Spotlight</h2>';
echo '<h2 style="text-align:center;">'.$cat_slug.' -- Slug</h2>';

echo "here: ".$cat_name." - ".$cat_slug;

echo do_shortcode('[wpb-product-slider title="" product_type="category" category="'.$cat_slug.'" items="3" items_desktop="3" items_desktop_small="3" items_tablet="2" items_mobile="1" width="320" height="300" crop="true" theme="ben-box" posts="50" orderby="rand" speed="2000" order="ASC"]'); ?>

But the $cat is always empty.
How can I do that ?

Sundaze 2 months 0 Answers 13 views 0

Leave an answer