Title: image.php conditional parent categories?
Last modified: August 19, 2016

---

# image.php conditional parent categories?

 *  [2bak860](https://wordpress.org/support/users/2bak860/)
 * (@2bak860)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/)
 * I’m trying to craft a conditional using parent categories, as it seems they’re
   the only way to get a category conditional on an attachment.php
 * There is no in_category_parent – is there a solution?
 * many thanks in advance,
 * R

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/#post-1297372)
 * Have you had a look at the [post_is_in_descendant_category function](http://codex.wordpress.org/Template_Tags/in_category#Testing_if_a_post_is_in_a_descendant_category)?
 *  Thread Starter [2bak860](https://wordpress.org/support/users/2bak860/)
 * (@2bak860)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/#post-1297455)
 * Thanks Esmi,
 * How would I make it form a conditional, so that I can show different content?
 *     ```
       <?php if ( in_category( 'fruit' ) || post_is_in_descendant_category( 11 ) ) { ?>
       	Fruit content
       <?php } elseif ( in_category( 'vegetable' ) || post_is_in_descendant_category( 12 ) ) { ?>
       Vegetable content
       <?php }
       ?>
       ```
   
 * And put this in the functions.php ?
 *     ```
       <?php
       /**
        * Tests if any of a post's assigned categories are descendants of target categories
        *
        * @param int|array $cats The target categories. Integer ID or array of integer IDs
        * @param int|object $_post The post. Omit to test the current post in the Loop or main query
        * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
        * @see get_term_by() You can get a category by name or slug, then pass ID to this function
        * @uses get_term_children() Passes $cats
        * @uses in_category() Passes $_post (can be empty)
        * @version 2.7
        * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
        */
       function post_is_in_descendant_category( $cats, $_post = null )
       {
       	foreach ( (array) $cats as $cat ) {
       		// get_term_children() accepts integer ID only
       		$descendants = get_term_children( (int) $cat, 'category');
       		if ( $descendants && in_category( $descendants, $_post ) )
       			return true;
       	}
       	return false;
       }
       ?>
       ```
   
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/#post-1297523)
 * That looks about right…
 *  Thread Starter [2bak860](https://wordpress.org/support/users/2bak860/)
 * (@2bak860)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/#post-1297527)
 * Unfortunately it doesn’t work in attachment.php – no idea why!
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/#post-1297540)
 * Ah – not considered it being used for an attachment. Judging from my tests (actually
   carried out in image.php), an attachment doesn’t have a category (post_category
   = 0). So I think what you need to do is to grab the cat_id of the post_parent–
   which you should be able to do via `$post->post_parent`.
 *  Thread Starter [2bak860](https://wordpress.org/support/users/2bak860/)
 * (@2bak860)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/#post-1297637)
 * Thanks Esmi – how would I change the conditional to take this into account?
 * Really appreciate your help, Ross
 *  Thread Starter [2bak860](https://wordpress.org/support/users/2bak860/)
 * (@2bak860)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/#post-1297644)
 * I’ve tried this and it does work – any advice? thanks, Ross
 *     ```
       <?php
   
       global $post;     
   
       if ( $post->post_parent == '3' ) { ?>
           3 hautecouture
   
       <?php } elseif( $post->post_parent == '8' ) { ?>
           8 lingerie 
   
       <?php } ?>
       ```
   
 *  Thread Starter [2bak860](https://wordpress.org/support/users/2bak860/)
 * (@2bak860)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/#post-1297661)
 * Hi Esmi – any advice on using a conditional for `$post->post_parent` ?
 * thanks, Ross
 *  Thread Starter [2bak860](https://wordpress.org/support/users/2bak860/)
 * (@2bak860)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/#post-1297674)
 * How can I grab the cat_id of the post_parent – with $post->post_parent.?
 * thanks, Ross

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

The topic ‘image.php conditional parent categories?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [2bak860](https://wordpress.org/support/users/2bak860/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/imagephp-conditional-parent-categories/#post-1297674)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
