I want to get the current category details that the user is on in category.php.
$category = get_the_category();
$slug = $category[0]->slug; // Why is this an array ?
In most cases (where there are no sub-categories) it returns and array of single length.
But if there are sub-categories it (parent category and ) returns an array of 2 or more.
http://domain.com/category/cat-name/ -> get_the_category() returns an array of 2
http://domain.com/category/cat-name/sub-cat-name/ -> get_the_category() returns an array of 2
You are using the wrong function. Try:
I don’t know exactly what you want to do with this information but you will get an object (stdClass) with ~15 items in it. You should be able to find what you need.
I’ve also got it from the following :
But I guess s-ha-dum’s solutions is better.
I normally use the following to retrieve the category ID of the current category archive:
$cat_id = get_query_var('cat')
to get the details, you can use:
$cat = get_category( $cat_id )
http://codex.wordpress.org/Function_Reference/get_category