Getting the ID of the current category in WordPress is actually quite simple. This will work in category archive pages or anywhere a category has been queried.
We are going to use
get_queried_object() to accomplish this task. The following script will get all the information about our queried object and put it in an array, $category. Then we are simply getting the
term_id from that array and echoing it.
Get Current Category ID
$category = get_queried_object();
Just place that code in any template file where a category has been queried, such as category archive pages, and you will be able to get the category id no problem. Another fun trick is if you are looking to display future posts in your query.
Tired of dealing with your slow WordPress website? Email me at firstname.lastname@example.org and mention the code #FreeHosting10 for two free months of lightning-fast WordPress hosting. We will even migrate you for free!