dekraan Posted January 2, 2013 Share Posted January 2, 2013 Hi there! I am currently trying to edit a wordpress theme, and with my limited knowledge, I got stuck first thing., I have just added a subpage to a page on my blog for the first time, and would love to display a list in the sidebar on the parent that displays the parent itself, and all children. On the childpages, this same list (parent+children) should be visible. So, let's say I have a page called: contact, with subpages like 'route' and 'form', it should look like this, on the parentpage (contact) and the children: Contact Route Form On pages that do not have children, there should be no list. Looking through the wordpress codex, I found the following code to add to my sidebar.php: <?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 } ?> This works perfect on a child page, displaying child and parent in a neat linkable list. But on the parent page it only displays the child. How can I also add the parent there? Thank you in advance for your help! Quote Link to comment https://forums.phpfreaks.com/topic/272618-wordpress-php-list-of-parent-an-children-in-sidebar/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.