• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Adrien Design

  • Blog
  • 3 Doors
You are here: Home / Web Development / WordPress / Categories & Taxonomies / Checking for a specific category

02/01/2022

Checking for a specific category

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:

  1. It checks what kind of content is being viewed — archive page or single post.
  2. 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( $cat->term_id === $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 for
function 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.

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( $cat->term_id === $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.

Trying to fiddle around with categories on pages can result in “trying to get value from a non-object” errors.

Filed Under: Categories & Taxonomies

About Adrien

I studied design in college, but I discovered what I really want to do when I started getting into web design and coding. The beautiful part about it is how you can study, practice, and learn every day; and you'll never reach the bottom of the well.

Every day working as a WordPress developer is challenging and fulfilling.

This is my personal site where I come to put down my thoughts and experiment with code ideas.

Primary Sidebar

About the Author

I studied design in college, but I discovered what I really want to do when I started getting into web design and coding. The beautiful part about it is how you can study, practice, and learn every day; and you'll never reach the bottom of the well.

Every day working as a WordPress developer is challenging and fulfilling.

This is my personal site where I come to put down my thoughts and experiment with code ideas.

Copyright © 2023 · Adrien Sanborn