Title: Page link tree
Last modified: August 31, 2016

---

# Page link tree

 *  Resolved [YOELO](https://wordpress.org/support/users/yoebo/)
 * (@yoebo)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/page-link-tree/)
 * Hi all, I have a lot of 4th-level child pages and I’d like to display all of 
   its parents as a tree e.g.
 *     ```
       Page 1
         Page 2 (i.e. parent of Page 3)
           Page 3
             Current page
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [CPC Mike](https://wordpress.org/support/users/homergz/)
 * (@homergz)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/page-link-tree/#post-6971320)
 * Try this:
 * [https://codex.wordpress.org/Function_Reference/wp_list_pages](https://codex.wordpress.org/Function_Reference/wp_list_pages)
 * Lots of example uses at the bottom of the page.
 *  Thread Starter [YOELO](https://wordpress.org/support/users/yoebo/)
 * (@yoebo)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/page-link-tree/#post-6971428)
 * Perfect thank you so much. I used:
 *     ```
       <?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 } ?>
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Page link tree’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [YOELO](https://wordpress.org/support/users/yoebo/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/page-link-tree/#post-6971428)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
