WordPress: Get Category ID for a Category Page

wp-pluginIt 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! 


13 Responses to WordPress: Get Category ID for a Category Page

  1. Cheap Wholesale Clothing May 14, 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.

  2. Laetitia May 20, 2009 at 3:12 pm #


    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 🙂 ).


    Have a great day 😉

  3. Star Quotes May 21, 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…

  4. Becca Lee July 29, 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.

  5. Byron Bennett July 30, 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!

  6. Steve Betts August 18, 2009 at 6:24 pm #

    I think what you need is: get_the_category(). It returns the current category on a category page.

  7. Dizi izle October 3, 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…

  8. Jermaine Maree October 9, 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 😉

  9. D February 15, 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.

  10. peetonn March 3, 2010 at 5:20 am #

    thanks a lot!!!!!! i’m getting mad while looking for answers in google 😎
    get current category wordpress

  11. seattle internet marketing June 7, 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.

  12. sunshine.by August 30, 2010 at 5:51 pm #

    Simple yet effective 🙂
    Thanks a lot!

  13. Jason Ashdown October 6, 2010 at 4:21 am #

    Worked for me! When posts are tagged in multiple categories, this is handy for retrieving the actual category page your on. Thanks 🙂

Powered by WordPress. Designed by Woo Themes