• Resolved roxer82

    (@roxer82)


    Is there an easy way to know if a post is under a parent category, no matter the child ones?
    Example:
    If my categories are:
    CAT 1
    └-CAT 1.1
    └-CAT 1.2
    ..└-CAT 1.2.1
    ..└-CAT 1.2.2
    ….└-CAT 1.2.2.1
    ….└-CAT 1.2.2.2
    CAT 2
    └-CAT 2.1
    ..└-CAT 2.1.1
    ..└-CAT 2.1.1
    └-CAT 2.2

    If a post is under “CAT 1.2.2.2” or “CAT 1” or “CAT 1.1” I want to get “True” if I ask if is in category “CAT 1”.

    Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter roxer82

    (@roxer82)

    Peter_L, thanks for your answer, but I can’t find in that link how to get a “true” if I ask in a post in “CAT 1.2.2.2” if it’s in “CAT 1”.
    I tried “post_is_in_descendant_category” but it crashes my webpage.

    True, sorry read your post to fast. I’ve been using following trick but I can’t promise it’s great code.

    <?php
    	$subcat = false;
    	$subcategories = get_categories( array( 'child_of' => ***put category id here*** ) ); // get subcats of this cat
    	if ( !empty($subcategories) ){ // if there are subcats
    		foreach ( $subcategories as $category ) { // loop array
    			if( is_category( $category->term_id ) ){ // is post in this subcat?
    				$subcat = true;
    			}
    		}
    	}
    	if ( in_category ( ***put category id here*** ) || $subcat ){
    		// returns true for category and subcategories
    	}
    ?>

    Thread Starter roxer82

    (@roxer82)

    Thanks Peter_L!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get "true" if I ask for a parent category of a post’ is closed to new replies.