Get current category ID of the active page Get current category ID of the active page wordpress wordpress

Get current category ID of the active page


If it is a category page,you can get id of current category by:

$category = get_category( get_query_var( 'cat' ) );$cat_id = $category->cat_ID;

If you want to get category id of any particular category on any page, try using :

$category_id = get_cat_ID('Category Name');


You can try using get_the_category():

$categories = get_the_category();$category_id = $categories[0]->cat_ID;


The oldest but fastest way you can use is:

$cat_id = get_query_var('cat');