Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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...

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Brilliant piece of code. Just what I needed. Thanks! –  rhand Mar 14 '12 at 14:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.