0

I use the following code to load subpages in a sidebar on all pages:

<?php $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0&depth=1'); if ($children) { ?> <ul id="three-menu"> <?php echo $children; ?> </ul> <?php } ?> 

It works well on all pages with sub pages or children. But what if I want this sidebar with children pages to load with parents]when there are no children? What code to add to another if or elseif? Basically I need to add a No children? Then loads parent pages..

Update:

At http://www.fldtrace.com/wordpress/how-to-display-parent-page-title-in-wordpress I found this code to load the parent page link:

<?php $parent_title = get_the_title($post->post_parent);?> <a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a> 

Which is great, but I need to list all parent pages, which are sub pages of the grand parent...

1 Answer 1

2

possible code:

<?php $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0&depth=1'); if ($children) { ?> <ul id="three-menu"> <?php echo $children; ?> </ul> <?php } //ends (if($children)// elseif($post->post_parent) { //if no children, try to get parent page and show siblings pages including the page itself $siblings = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0&depth=1'); if ($siblings) { ?> <ul id="three-menu"> <?php echo $siblings; ?> </ul> <?php } //ends if($siblings)// ?> <?php } else { //optional: if no children and if no parent, then show all top level pages $pagelist = wp_list_pages('title_li=&echo=0&depth=1'); if ($pagelist) { ?> <ul id="three-menu"> <?php echo $pagelist; ?> </ul> <?php } //ends if($pagelist)// ?> <?php } ?> 

contains a lot of redundant coding to keep the structure recognizable simple.

1
  • Brilliant piece of code. Just what I needed. Thanks! Commented Mar 14, 2012 at 14:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.