RonnieT RonnieT - 2 years ago 118
PHP Question

Get current category ID of the active page

Looking to pull the category ID of a specific page in WordPress that is listing all posts using that specific category. Tried the below but not working. I am able to get the category name using


$category = single_term_title("", false);
$catid = get_cat_ID( $category );

is displaying "Entertainment" for example. But I also need the ID of "Entertainment". How would I go about this?

Answer Source

You can try using get_the_category():

$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download