How to Check if Category is a Subcategory

This is a useful function I use for building conditional statements to check whether a category is a subcategory (.e. has a parent).

In your functions.php file insert the following:

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

Then in your theme you can use the conditional as follows:

if ( is_subcategory() ) {
// do required actions

If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.

About Jean Galea

Jean Galea is a WordPress developer, entrepreneur and padel player. He is the founder of WP Mayor, the plugins WP RSS Aggregator and EDD Bookings, as well as the podcast. His personal blog can be found at

Related Articles