WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] post_is_in_descendant_category Help (6 posts)

  1. StyledThemes
    Member
    Posted 1 year ago #

    I tried this from the codex page:
    post_is_in_descendant_category

    My Code:
    <?php if ( is_category( 'portfolio' ) || post_is_in_descendant_category( 12 ) ) : ?>

    I have it so that any posts in the parent category of "Portfolio" and in sub categories of this parent, will use a different template I created. The code above works, but the problem is that I cannot use the ID, it must be a name "Portfolio". There is an example if one wants to use names, but it's simply not working for me. Is there a way to use the name and not the ID?

    I'm making a theme for others to use so using an ID that will more likely be different for each user is not viable here. My setup tutorial will instruct them to name the parent category (if they have other cats) as "Portfolio" to make this easier.

    Thanks in advance.

  2. bythegram
    Member
    Posted 1 year ago #

    I believe what you need is

    if ( $category_to_check = get_term_by( 'name', 'portfolio', 'category' )) {
      if ( is_category( 'portfolio' ) || post_is_in_descendant_category($category_to_check->term_id) ) {
  3. StyledThemes
    Member
    Posted 1 year ago #

    Thanks for the reply.... however, I tried that and I get a server error. I just found this solution which worked and it has to do with that functions code on the codex that is the issue:

    <?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
     */
    if ( ! function_exists( 'post_is_in_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;
    	}
    }
    ?>

    I found this from Michal Ochman http://blog.scur.pl/ which had a modified version of the above code. I then used this in my category file:
    <?php if ( is_category( 'portfolio' ) || post_is_in_descendant_category( 'portfolio' ) ) : ?>
    I had to use the : ?> at the end because I have an "if" "else" in my file.

    Now everything works perfectly and I didn't have to use a custom post type or custom taxonomy for portfolio categories...while achieving different layouts also for the full post view (single) from portfolio categories :)

  4. bythegram
    Member
    Posted 1 year ago #

    yep makes sense, sorry I should have included the full solution.

  5. StyledThemes
    Member
    Posted 1 year ago #

    no worries... now a solution is here for everyone else :)

  6. ritalcnyc
    Member
    Posted 1 year ago #

    Sorry to reopen this, but as a non-coder, I'm at a loss how I would include descendants of three categories -- For example, say I wanted to target descendants of the portfolio, news and arts categories and assign a different sidebar? Do I make an array or use ifelse statements? What's the syntax?

    Also, would it be considered bad form to use the regular single.php and put this if statement at the get_sidebar() tag?

    Thanks for any clarification that could be provided.

Topic Closed

This topic has been closed to new replies.

About this Topic