phpwordpress

WordPress - Remove Ancestor Listing from this


I took this code directly & unedited from the WordPress Codex and it essentially lists the children of the current page. However, I would like to remove the listing of the parent / ancestor. Any help would be most appreciated.

Thanks!

<?php
//if the post has a parent
if($post->post_parent){
  //collect ancestor pages
  $relations = get_post_ancestors($post->ID);
  //get child pages
  $result = $wpdb->get_results( "SELECT ID FROM wp_posts WHERE post_parent = $post->ID AND post_type='page'" );
  if ($result){
    foreach($result as $pageID){
      array_push($relations, $pageID->ID);
    }
  }
  //add current post to pages
  array_push($relations, $post->ID);
  //get comma delimited list of children and parents and self
  $relations_string = implode(",",$relations);
  //use include to list only the collected pages. 
  $sidelinks = wp_list_pages("title_li=&echo=0&include=".$relations_string);
}else{
  // display only main level and children
  $sidelinks = wp_list_pages("title_li=&echo=0&depth=1&child_of=".$post->ID);
}

if ($sidelinks) { ?>
  <h2><?php the_title(); ?></h2>
  <ul>
    <?php //links in <li> tags
    echo $sidelinks; ?>
  </ul>         
<?php } ?>

Solution

  • I think there is no need to complicate the problem. If you want to get children of current post, just check if it has children and if yes, show them. You need to adjust to your needs of course. But is just shows how to get them.

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

    This link is useful for what you want to do: http://codex.wordpress.org/Function_Reference/wp_list_pages