20 posts
  • Bought between 1 and 9 items
  • Has been a member for 5-6 years
dekraan says

Hi there all!

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.

Basically, what I am looking for is:

1. Listing parent and children as links in the sidebar of a page that has children. 2. Keeping that list of parent an children links when you go to the childpage. 3. No list of links on a page that has no childpages.

Looking through the codex, I found the following:

<?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; ?> </li></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!

40 posts
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Bought between 1 and 9 items
orbitalplugins says

Hi, try changing the second smaller code block to this:

<h2><?php the_title(); ?></h2>
<?php if ($sidelinks) : ?>
    <ul>
        <?php echo $sidelinks; ?>
    </ul>   
<?php endif; ?>
20 posts
  • Bought between 1 and 9 items
  • Has been a member for 5-6 years
dekraan says

Hi, try changing the second smaller code block to this:
<h2><?php the_title(); ?></h2>
<?php if ($sidelinks) : ?>
    <ul>
        <?php echo $sidelinks; ?>
    </ul>   
<?php endif; ?>

Hi there,thank you for your reply. Unfortunately, that did not do the trick!

Just to be sure, this is the code I used first:

<?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); } <h2><?php the_title(); ?></h2> <?php if ($sidelinks) : ?> <ul> <?php echo $sidelinks; ?> </ul> <?php endif; ?>

But that messed up my code, to the point where only my header was visible. So I added the line

if ($sidelinks) { ?>

in front of the latest small block. But alas, that did not work either…

EDIT: can’t seem to get the code inside code, although I placed it between two tags. Sorry bout that!

40 posts
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Bought between 1 and 9 items
orbitalplugins says

I don’t think it worked for you because you didn’t close a php tag, try this:

<?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);
}
?>

<h2><?php the_title(); ?></h2>
<?php if ($sidelinks) : ?>
    <ul>
        <?php echo $sidelinks; ?>
    </ul>
<?php endif; ?>

You can post code by typing this: <pre><code>[paste code here]</code></pre>

40 posts
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Bought between 1 and 9 items
orbitalplugins says
  • double post sorry :/
20 posts
  • Bought between 1 and 9 items
  • Has been a member for 5-6 years
dekraan says

No problem, and thank you for your help :-)

I think I found a different way of getting it done, puzzling a bit with other bits and pieces I found round the net. Although I do not really understand what I did and how I did it :-)

 
<?php if ( is_page() ) { ?>
<ul><?php
  if($post->post_parent){
    $parent=get_post($post->post_parent);
    $children = '<li><a href="'.get_permalink($post->post_parent).'">'.$parent->post_title.'</a></li>';
    $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
  }else{
    $children = '<li><a href="'.get_permalink($post).'">'.$post->post_title.'</a></li>';
    $children .= wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
  }
echo $children; ?>
</ul>
<?php } ?>

It seems better, because it is shorter. I tried this one before, but the problem was that on the sidebar of my homepage, it displayed a blogpost. But by adding that first line of code, I seem to have limited it to pages!

by
by
by
by
by
by