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
1 2 |
$category = get_queried_object(); echo $category->term_id; |
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.
5 Comments on “Get ID of Current Category – WordPress”
Thanks man, these two lines of magic were very useful.
Thanks, Bro….this is exact code which I need
Here we are, so simple so useful,
Thanks!
Awesome!!
Really Works..!!
Thanks 🙂
This is what exactly I’m looking for. Thank you!