Getting the category ID in WordPress
There is quite a bit of documentation in the WordPress site about categories, specifically how to retrieve data about them. Unfortunately all the available methods seem to focus on getting category data from a post, but it's nearly impossible to figure out the category you are looking at when viewing the category page itself (i.e. the special "archive page" that lists all posts in a category).
The function is_category()
falls short, since it only indicates whether or not the current page is a category page, but not which one. And get_cat_id()
, get_category()
, get_the_category()
, and the half-dozen other "obviously"-named functions either require that you already know something about the category or provide information about a post.
So, the solution?
if (is_category()) {
$categoryId = $GLOBALS["cat"];
}
Yes, it's a global variable...