Support » Fixing WordPress » Creating an is_subcategory() conditional function

  • jayred

    (@jayred)


    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)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Creating an is_subcategory() conditional function’ is closed to new replies.