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 single_term_title.

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

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

Solutions

You can try using get_the_category():

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

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');

The oldest but fastest way you can use is:

$cat_id = get_query_var('cat');

Similar questions

Why is my site ssl not active but active in WordPress panel?
Hi I'm Mohammad Reza I wanted to know why ssl is not active when I visit to my site but it is active in my wordpress panel? It's my site (not active ssl): https://topmoviex.ir/ This page is active ssl: https://topmoviex.ir/پروفایل-کاربری/
Remove Active Class from Parent when Child is Active in Wordpress SubMenu
I have a menu with secondary items in Wordpress. It works great except the active class should only be on the child when that page is active. Right now it is active on both. What is the best way to remove it? jQuery, Filter, or Editing the Walker. This is Bootstrap based theme so it is using the bootstrap nav walker from GitHub (wp-bootstrap/wp-boo...
How to fix Warning: Use of undefined constant active - assumed 'active' (this will throw an Error in a future version of PHP)
I'm currently working on a WirdPress Theme project using Bootstrap v5.0.2 and PHP 7.3.28. I'm tryng to insert the Bootstrap Carousel Component in a custom widget but when I debug the code I recive this "Warning: Use of undefined constant active - assumed 'active' (this will throw an Error in a future version of PHP) in ... on line 70" Thi...
WordPress Category, Child Category, Active Category Problems
`Let's say I went to a category page. There is some content on this category page. I would like to show all the child categories of the parent category of this category below the content or under this category page. I would also like to show the description and thumbnail image of the category. How do I do that? Then if a user goes to a category pag...
Display current category without an active link in wp_list_categories
I appreciate your time in looking into this for me. I'm using wp_list_categories tag to display a list of categories in my navigation menu and I'd like to disable the link to the current category when viewing said category page. My main reason is that the color attribute in my CSS style for li.cat-item a is overriding the color attribute for my CSS...
Add active class to woocommerce product category current-menu-item
I've used the below code to add the active class to menu items. The site is 99% Ecomm using Woocommerce and I'm using product categories as top level navigation items. The below code works with almost all items except when being active on a product sub-category. add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2); ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.