WordPress.org

Ready to get started?Download WordPress

Forums

image.php conditional parent categories? (10 posts)

  1. 2bak860
    Member
    Posted 4 years ago #

    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

  2. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    Have you had a look at the post_is_in_descendant_category function?

  3. 2bak860
    Member
    Posted 4 years ago #

    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;
    }
    ?>
  4. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    That looks about right...

  5. 2bak860
    Member
    Posted 4 years ago #

    Unfortunately it doesn't work in attachment.php - no idea why!

  6. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    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.

  7. 2bak860
    Member
    Posted 4 years ago #

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

    Really appreciate your help, Ross

  8. 2bak860
    Member
    Posted 4 years ago #

    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 } ?>
  9. 2bak860
    Member
    Posted 4 years ago #

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

    thanks, Ross

  10. 2bak860
    Member
    Posted 4 years ago #

    How can I grab the cat_id of the post_parent - with $post->post_parent.?

    thanks, Ross

Topic Closed

This topic has been closed to new replies.

About this Topic