Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    2
    Thanks
    0
    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:
    <?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!
    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
  •