Support » Fixing WordPress » How to display children but not grandchildren

  • Hello,

    I’d like to list the child elements of a particular category without listing its grandchildren. Currently, my code lists all children and grandchildren. This is the code that I’m using:

    <?php
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    if ($children) {?>
    <?php echo $children;?>
    <?php }?>

    Can anyone lend me a hand? Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Try:

    <?php
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");
    if ($children) {?>
    <?php echo $children;?>
    <?php }?>

    I’ve not tried using the depth parameter in conjunction with child_of, so you may have to use a value of 2 to get it to work correctly.

    esmi, that was timely and perfect. thx.

    Note the addition of “depth=1” on line 5 to hide the grandchildren.

    <?php
    if($post->post_parent)
    $children = wp_list_pages(“title_li=&sort_column=menu_order&child_of=”.$post->post_parent.”&echo=0&depth=1″);
    else
    $children = wp_list_pages(“title_li=&sort_column=menu_order&child_of=”.$post->ID.”&echo=0&depth=1“);
    if ($children) { ?>
    <div class=”widget widget_pages”>

      <?php echo $children; ?>

    </div>
    <?php }

    Have the same problem. But for some reason the code displays children and grandchildren?

    <?php
      $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=1&depth=1');
      if ($children) { ?>
    
    <ul>
      <?php echo $children; ?>
      </ul>
      <?php } ?>

    Ignore the above, I applied it to the wrong template 🙂

    I’m new to WP. Where does the above code get put? Do I need to make a separate template for it, and if so, how do I specify a particular category to use that template? If there are docs to guide me through this, a URL would be appreciated.

    Thanks.

    Resolved using wp_list_categories and category specific template

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to display children but not grandchildren’ is closed to new replies.