Sometimes I like to use Advanced Custom Fields to set special categories that need unique layout or content treatment. This allows me to get the ID of the category and apply body classes and use hooks for certain conditions related to those categories.
The problem
While WordPress provides lots of conditional tools to check the category status, these individual tools can sometimes fall short with more specific needs.
The broad question I’m seeking to answer is this: “does the current page being viewed fit into category XYZ, be it a post or an archive page?”
Some tools available
Just to mention a couple of conditional tags available:
is_category()
allows you to check if the current category/archive page is in the category you’re checking.in_category()
will check an array of category IDs or names against the current post’s categories. (not used in the solution, but it’s a nice function anyway)is_archive()
checks if the current page is a category or archive page.
These are all useful, but none of them alone can do the task at hand.
For this solution, we’ll have to wrap some of these tools in a function that gives us a little extra flexibility.
The full solution
A function that checks a category ID against any content type, excluding pages
The function takes 2 steps:
- It checks what kind of content is being viewed — archive page or single post.
- Then checks the current page against the category-in-question.
function ades_is_current_cat($id=null) {
if( is_archive() && is_category($id) ) {
return true;
} elseif( is_single() ) {
$post_cats = get_the_category();
foreach($post_cats as $cat) {
if( $id === $cat->term_id ) return true;
}
}
return false;
}
What’s happening here?
The function takes a single parameter, which is the category ID we’d like to test forfunction ades_is_current_cat($id=null)
.
We’d like to know if the page/post being viewed is assigned this category.
1. Archive & Category pages
First we check if this is an archive or category page.
If so, is_category($id)
checks if this page is for the category in question.
if( is_archive() && is_category($id) ) {
return true;
}
2. Single posts
If this is not a category/archive page, then the function checks if it’s a single post.
} elseif( is_single() ) {
.
If so, we get an array of category objects for every category assigned to the current post.
$post_cats = get_the_category();
With a foreach loop, we check each array item (i.e. each category for the post).
If any category in the array is a match, the function returns true
, meaning that the category in question is, in fact assigned to the current post.
foreach($post_cats as $cat) {
if( $id === $cat->term_id ) return true;
}
3. Pages
If the current post being viewed fails the checks for both archives and single posts, a simple false
is returned. I never use taxonomies with pages, so the function simply moves on if we find ourselves on a page.
This is to avoid the “trying to get value from a non-object” error that can result from trying to work with non-existent category objects on pages.