WordPress.org

Ready to get started?Download WordPress

Forums

If is 3rd Level of Category (8 posts)

  1. equalmark
    Member
    Posted 4 years ago #

    I have the following category structure for a site that I am working on:

    2008
    - Group 1
    -- Review 1
    -- Review 2
    - Group 2
    -- Review 1
    -- Review 2
    2009
    - Group 1
    -- Review 1
    -- Review 2
    - Group 2
    -- Review 1
    -- Review 2

    I want to display the category listing page for all the categories called "review" in a different way. Therefore how to do I check for whether the category is a 3rd level category i.e. it has a parent and grandparent?

  2. equalmark
    Member
    Posted 4 years ago #

    Getting round this another way I have managed to fix this however I am now using the code below in order to display the category names of child categories of the current category.

    <?php $descendants = get_categories(array('child_of' => 6)); ?>
    
     <?php foreach ($descendants as $child) { ?>
    
         <li><?php echo $child->cat_name; ?></li>
    
    <?php } ?>

    My questions is, is how do I alter this so that it only brings back the immediate child categories of the current category, rather than child and grandchild categories?

  3. MichaelH
    Member
    Posted 4 years ago #

    More to your first question:

    <?php
    $test_for_level = intval(3);
    $last_level = 0;
    $cat_to_test = 6;
    $category=get_category($cat_to_test);
    for ( $counter = 1; $counter <= $test_for_level; $counter += 1) {
      if ($category->category_parent) {
        $category=get_category($category->category_parent);
        $last_level = $counter;
      }
    }
    $last_level +=1;
    if ($last_level == $test_for_level) {
      echo '<p>Category ' . $cat_to_test . ' is at level ' .$last_level . '</p>';
    } else {
      echo '<p>Category ' . $cat_to_test . ' is NOT at level ' .$test_for_level . '</p>';
    }
    ?>
  4. idowens
    Member
    Posted 4 years ago #

    Add depth=x, where x is the number of levels you wish to show.

  5. equalmark
    Member
    Posted 4 years ago #

    Thanks for that MichaelH - works a treat.

    Regarding my second question. The depth function doesn't seem to work. It is not a parameter of the function according to this page?

    http://codex.wordpress.org/Function_Reference/get_categories

    Anyone have any ideas?

  6. MichaelH
    Member
    Posted 4 years ago #

    That depth= seems to only work with wp_list_categories and wp_dropdow_categories and both pass that argument to walk_category_tree.

  7. equalmark
    Member
    Posted 4 years ago #

    At the moment when I click on category 2008 / 2009 it show the immediate children (Group 1, Group 2) but also the grand children (Review 1, Review 2). It is OK when on the Group 1 or Group 2 cat pages. How do I stop it returning Grand Children?

  8. idowens
    Member
    Posted 4 years ago #

    Michael is right, the depth var only works with the two he listed...my apologies.

    I'm not sure how to limit depth with the wp_get param.

Topic Closed

This topic has been closed to new replies.

About this Topic