• 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)
  • Have you had a look at the post_is_in_descendant_category function?

    Thread Starter 2bak860

    (@2bak860)

    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;
    }
    ?>

    That looks about right…

    Thread Starter 2bak860

    (@2bak860)

    Unfortunately it doesn’t work in attachment.php – no idea why!

    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

    (@2bak860)

    Thanks Esmi – how would I change the conditional to take this into account?

    Really appreciate your help, Ross

    Thread Starter 2bak860

    (@2bak860)

    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

    (@2bak860)

    Hi Esmi – any advice on using a conditional for $post->post_parent ?

    thanks, Ross

    Thread Starter 2bak860

    (@2bak860)

    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.