Adding list of parent, current and child in sidebar
-
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.
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; ?> </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?
I am also curious how to delete the ‘title’ this list has. Now it just adds the parents name in plain text…
Thank you in advance for your help!
-
This is getting closer to the sollution:
<?php if($post->post_parent) { // if the current page has a parent $parent_title = get_the_title($post->post_parent); $parent_link = get_permalink($post->post_parent); $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");?> <ul> <li><?php echo "<a href='" . $parent_link . "'>" . $parent_title . "</a>"; ?> </li> <?php } else { // if the current page is the parent $parent_title = get_the_title($post->ID); $parent_link = get_permalink($post->ID); $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); ?> <ul> <li class="current_page_item"><a href="#"><?php the_title(); ?> Main</a> </li> <?php } echo $children; ?> </ul>
Although this also displayes the first post title on my homepage sidebar. And that should be excluded…
Can you then use another conditional statement using the is_home function as the argument?
I have no idea what you are talking about, unfortunately. But that is all my fault 🙂
Do you mean excluding the home from the equasion? That would be great, but how to do that?
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.Found a new one, that almost seems to sort it out. Only problem with the code below, is that it doesnt display the parent link on the parent page… is there a way to combine something from these 3 examples of code to make my idea work?
<?php /* if the current pages has a parent, i.e. we are on a subpage */ if($post->post_parent){ $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable } /* else if the current page does not have a parent, i.e. this is a top level page */ else { $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); // form a list of the children of the current page } /* if we ended up with any pages from the queries above */ if ($children) { ?> <ul class="submenu"> <?php echo $children; /*print list of pages*/ ?> </ul> <?php } ?>
For anyone still interested: this seems to do the trick:
<?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 } ?>
The secret was starting out with a little is_page code!
- The topic ‘Adding list of parent, current and child in sidebar’ is closed to new replies.