Question

I keep finding tutorials on listing all the slugs on the internet but can't seem a function or line of code that can echo/return me the slug of the category i'm on so I can save it on a variable.

Does anyone know what it is? I need to be able to use it in archive-product.php

Thanks in advance

Was it helpful?

Solution 2

$slugs = array();
foreach( (get_the_category()) as $category ) { 
    array_push( $slugs, $category->slug );
} 
var_dump( $slugs );

(untested)

http://codex.wordpress.org/Function_Reference/get_the_category

Edit:

Tested and works correctly. Try adding to archive.php (temporarily) and make sure your url is something like: mysite.com/category/uncategorized/ . I suspect you may not be calling the correct template file or perhaps not actually browsing any categories.

OTHER TIPS

The category slug should be available in global variable $wp_query.

Try this in yourtheme/woocommerce/archive-product.php:

global $wp_query;
$cat_slug = $wp_query->query_vars['product_cat'];
echo $cat_slug;

The other method with get_the_category() may work for normal posts in archive.php but I had no success with it in archive-product.php.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top