Get current category ID

Wpsnipp – 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.

Adding this PHP code to your functions.php of your wordpress theme will let you return the current category ID number.

 

 

 

<?php
function currentCatID(){
global $wp_query;
if(is_category() || is_single()){
$cat_ID = get_query_var('cat');
}
return $cat_ID;
}
?>


<?
echo currentCatID();
?>

Be Sociable, Share!

Leave a comment