WordPress.org

Ready to get started?Download WordPress

Forums

Childs child of parent category (6 posts)

  1. jabbamonkey
    Member
    Posted 3 months ago #

    I'm trying to create a conditional statement, for when a category is a sub-sub-category. So, not just 1 level deep, but 2 levels deep. So, I don't just want to see if I am in a child category of a parent category, but a grandchild of that category.

    For example... if my site was a list of professions, then I might have the following categories

    1. Professions (main category - parent)
         a) Home & Garden (subcategory - child)
              i) Gardeners (sub-subcategory - grandchild)

    So, when the user is on the "Gardener's" category page (or any sub-subcategory page), then I can show something (with the conditional statement).

    I checked the codex, and that shows something for looking for something in as a "descendent" of a category... but that wont work for "two levels" deep (a "descendent's descendent").

    // Post is assigned to "fruit" category or any descendant of "fruit" category?
    <?php if ( in_category( 'fruit' ) || post_is_in_descendant_category( 11 ) ) {
    	// These are all fruits…
    }
    ?>

    Can someone help? Thanks!

  2. jabbamonkey
    Member
    Posted 3 months ago #

    Since noone has answered in the last week, can someone help me figure out an alternative way to find a solution to this problem?

    Is there a way to check if the page is in a main parent category (i.e. Professions), and if so, then check and see if it's any sub category of the parent (i.e. like Home & Garden). If it is, then keep doing what its doing. But, if not, then it must be one of the sub categories children (i.e. Gardeners).

    I know there is no is_child function, but if there was, here is something that might work... (and I need something that actual works with actual functions)

    if(in_category('professions') {
       if(!is_child('professions)) {
       }
    }

    This sounds simpler (but not ideal) and should solve the problem I am having.

  3. jabbamonkey
    Member
    Posted 3 months ago #

    Since noone has answered in the last week, can someone help me figure out an alternative way to find a solution to this problem?

    Is there a way to check if the page is in a main parent category (i.e. Professions), and if so, then check and see if it's any sub category of the parent (i.e. like Home & Garden). If it is, then keep doing what its doing. But, if not, then it must be one of the sub categories children (i.e. Gardeners).

    I know there is no is_child function, but if there was, here is something that might work... (and I need something that actual works with actual functions)

    if(in_category('professions') {
       if(!is_child('professions)) {
       }
    }

    This sounds simpler (but not ideal) and should solve the problem I am having.

  4. elisabetha
    Member
    Posted 3 months ago #

    I don't know exactly if this hleps, but maybe you must look the other way, so not grandchild but grandparent. I used this for showing titles:
    <?php //variables
    $grandparent = $parent->post_parent;
    $grandparent_title = get_the_title($grandparent);
    ;?>

  5. jabbamonkey
    Member
    Posted 3 months ago #

    That only gives the title of the FIRST post in that category....

    <?php
    	$grandparent = $parent->post_parent;
    	$grandparent_title = get_the_title($grandparent);
    	echo "<!-- Grandparent: ".$grandparent_title." -->";
    ?>
  6. jabbamonkey
    Member
    Posted 3 months ago #

    Think that I may have solved this....

    <?php
    // Get Parent ID of current CAT
    $parentobj = get_category($cat);
    $ParentCatId = $parentobj->parent; 
    
    // Get Grandparent ID
    $grandparentobj = get_category($ParentCatId);
    $GrandparentCatId = $grandparentobj->parent;
    
    // If the Grandparent ID is 299 ...
    if($GrandparentCatId=='299') { 
    
    }
    ?>

Reply

You must log in to post.

About this Topic