Posted By

mesh606 on 05/27/11


Tagged


Versions (?)

wp-grandchild list


 / Published in: PHP
 

  1. <?php
  2. //if the post has a parent
  3. if($post->post_parent){
  4. //collect ancestor pages
  5. $relations = get_post_ancestors($post->ID);
  6. //get child pages
  7. $result = $wpdb->get_results( "SELECT ID FROM wp_posts WHERE post_parent = $post->ID AND post_type='page'" );
  8. if ($result){
  9. foreach($result as $pageID){
  10. array_push($relations, $pageID->ID);
  11. }
  12. }
  13. //add current post to pages
  14. array_push($relations, $post->ID);
  15. //get comma delimited list of children and parents and self
  16. $relations_string = implode(",",$relations);
  17. //use include to list only the collected pages.
  18. $sidelinks = wp_list_pages("title_li=&echo=0&include=".$relations_string);
  19. }else{
  20. // display only main level and children
  21. $sidelinks = wp_list_pages("title_li=&echo=0&depth=1&child_of=".$post->ID);
  22. }
  23.  
  24. if ($sidelinks) { ?>
  25. <ul id="parent"><li><?php the_title(); ?></li></ul>
  26. <ul>
  27. <?php echo $sidelinks; ?>
  28. </ul>
  29. <?php } ?>

Report this snippet  

You need to login to post a comment.