WordPress: Get Category ID for a Category Page
It shouldn’t be this hard!! Here is a quick function for getting the Category ID for a WordPress Category Page. Once you get the Category ID, a world of possbilities opens up. You can get the category object and all the info you need related to that Cat. I can’t believe how hard it was to find this on Google. Ultimately, I had to piece together a couple of different posts to get to this since they weren’t quite right.
If you’ve got a better way of getting this…please share! Here’s what I came up with:
function getCurrentCatID(){
global $wp_query;
if(is_category() || is_single()){
$cat_ID = get_query_var('cat');
}
return $cat_ID;
}
//To get the full category object, try this:
$cat = get_category(get_query_var('cat'),false);
Uggg…that shouldn’t have been so hard to find. Big ‘G’, you treated me bad!
[Follow-up] - as Steve notes below, try get_the_category() and see if that does what you need before implementing the function above. Thanks, Steve!
Cheers,
Byron
Popularity: 29%


May 14th, 2009 at 1:50 pm
Thanks for the sharing. Will try out the code on my new site. Need to exclude certain category from showing. I think the function you wrote to get the Cat ID will help.
May 20th, 2009 at 3:12 pm
Hi,
I searched a lot for a useful function like this, but as I cannot use yours (your function does not return the current cat ID when I have child cats in the current parent cat), I show you mine (don’t know if it’s better or not indeed but it works
).
term_id;
}
?>
Have a great day
May 21st, 2009 at 3:59 pm
Thanks a lot, it works like a charm!
Logically the category id should be stored in a variable somewhere when you’re viewing a category page…
July 29th, 2009 at 4:58 pm
I’ve spent way too long looking for this as well. As it turns out, $cat contains the category ID on my site’s category pages, but maybe my theme is setting it.
July 30th, 2009 at 7:34 am
Hi Becca,
$cat worked for me as well. I did a quick Google, but didn’t see a reference to it, so it may or may not be a solid method…but as long as it work!!!
Thanks for the tip!
Byron
August 18th, 2009 at 6:24 pm
I think what you need is: get_the_category(). It returns the current category on a category page.
October 3rd, 2009 at 11:08 am
Thanks a lot, it works like a charm!
Logically the category id should be stored in a variable somewhere when you’re viewing a category page…
October 9th, 2009 at 7:08 pm
This was SUPER helpful. Thanks so much for sharing! I definitely owe you one. If you’d like a free premium theme, let me know
February 15th, 2010 at 2:22 am
This works really well. I am using it to determine which level my subcategories are on.
Thank you for your insight … and google searchability.
March 3rd, 2010 at 5:20 am
thanks a lot!!!!!! i’m getting mad while looking for answers in google
get current category wordpress
June 7th, 2010 at 1:37 pm
get_the_category() is used for listing all of the categories, whereas this function is particularity useful when you get into advanced post setups that revolve around conditional post/category styling.