WordPress Get ID of Current Category

Getting the ID of the current category in WordPress is actually quite simple.

How can I retrieve slug of current taxonomy being viewed in taxonomy template?

To further explain, lets say I am viewing category flowers so how can I retrieve slug of category flowers?

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.

You can get the term object of the category you’re viewing with get_queried_object(). That will contain the slug.

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


Also published on Medium.

Leave a Reply

Your email address will not be published. Required fields are marked *