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();
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.
Latest posts by admin (see all)
- Install and Running Supervisord in virtualenv python 2.7 - January 24, 2019
- Urgent fix: Briefly unavailable for scheduled maintenance. Check back in a minute. - January 22, 2019
- Change WordPress post date format to time ago - January 21, 2019
Also published on Medium.