Title: Creating an is_subcategory() conditional function
Last modified: August 19, 2016

---

# Creating an is_subcategory() conditional function

 *  [jayred](https://wordpress.org/support/users/jayred/)
 * (@jayred)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/creating-an-is_subcategory-conditional-function/)
 * So while there are clearly documented ways to check if the user is currently 
   viewing a subpage, I needed a way to check if the user is currently viewing a
   category page where the category they were viewing is a subcategory (any subcategory,
   don’t care which one).
 * After struggling around with the idea for a while (and finding NOTHING online),
   I came up with the following code that seems to work. I’m curious, however, if
   anyone has any suggestions for a better way to do it.
 *     ```
       function is_subcategory(){
       		global $wpdb;
   
       		$root_cats = $wpdb->get_results("SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = 'category' AND parent = 0");
       		$cat_list = array();
       		foreach( $root_cats as $cat ){
       			array_push( $cat_list, $cat->term_id);
       		}
       		$is_root = is_category( $cat_list );
       		if( $is_root ) return false; else return true;
       	}
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [alanft](https://wordpress.org/support/users/alanft/)
 * (@alanft)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/creating-an-is_subcategory-conditional-function/#post-1149760)
 * [http://codex.wordpress.org/Function_Reference/get_category_parents](http://codex.wordpress.org/Function_Reference/get_category_parents)
 * or
 * [http://codex.wordpress.org/Function_Reference/get_term_children](http://codex.wordpress.org/Function_Reference/get_term_children)
 * may be useful to you
 *  Thread Starter [jayred](https://wordpress.org/support/users/jayred/)
 * (@jayred)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/creating-an-is_subcategory-conditional-function/#post-1149860)
 * As best as I can understand the documentation for those two functions, you have
   to pass them a category id, which I don’t know of a way to get automatically 
   based on the category page the user is currently viewing.
 *  [alanft](https://wordpress.org/support/users/alanft/)
 * (@alanft)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/creating-an-is_subcategory-conditional-function/#post-1149890)
 * you can use the variable $cat (set by WP) for the category of the current category
   page
 *  [anthonylrivera](https://wordpress.org/support/users/anthonylrivera/)
 * (@anthonylrivera)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/creating-an-is_subcategory-conditional-function/#post-1150037)
 * Hey I came across the same issue today and this is what I ended up with and seemed
   to work.
 *     ```
       function is_subcategory (){
           $cat = get_query_var('cat');
           $category = get_category($cat);
           return ( $category->parent == '0' ) ? true : false;
       }
       ```
   
 * which can be used in templates like any other conditional tag.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Creating an is_subcategory() conditional function’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [conditional tags](https://wordpress.org/support/topic-tag/conditional-tags/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [subcategory](https://wordpress.org/support/topic-tag/subcategory/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 3 participants
 * Last reply from: [anthonylrivera](https://wordpress.org/support/users/anthonylrivera/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/creating-an-is_subcategory-conditional-function/#post-1150037)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
