Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Thanked 0 Times in 0 Posts

    Wordpress: sidebar list with parent and children

    Hi there!

    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 Code:
    //if the post has a 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 as $pageID){
    //add current post to pages
    //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);
    // 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>
        <?php //links in <li> tags
    echo $sidelinks?>
    <?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!
    Last edited by DeKraan; 01-02-2013 at 08:03 AM.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts