Ok, I just spent the better part of ALL DAY trying to figure this out.

I want my wordpress category.php template to be able to detect whether the category it is serving is a parent category or subcategory. THATS IT. And without having to hard code any category ID’s. After perusing the forums and codex for a couple of hours, the marriage of these two functions seemed to get the job done.

get_query_var is the variable stored in the query that tells category.php what category to serve.

get_category retrieves category data given a category ID or category object.

Here’s the function that goes in your functions.php file:

function is_subcategory (){
    $cat = get_query_var('cat');
    $category = get_category($cat);
    return ( $category->parent == '0' ) ? true : false;

Then you can use this in your category.php file to determine if the category you are serving is a parent category or subcategory:

if ( is_subcategory() ):
     //displaying subcategory
    //displaying parent category

Hope this helps some people out there.

*note: both code snippets should be wrapped inside php brackets. <?php ?>

One reply on “is_subcategory()”

Comments are closed.