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 have a parent page called "About". This page has three child pages "Members", "Information", "Contact".

I want to list all the child-pages on all four pages, also the parent-page. So right now I have this which lists all the child-pages only on the parent-page.

<?php
        if ( !$post->post_parent ) {
            echo '<ul id="subpages" class="wrapper">';
                wp_list_pages("title_li=&child_of=".$post->ID."&link_before=— ");
            echo '</ul>';
        }
    ?>

But I want all the pages also listed on each individual child page.

How do I do that?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

This function will do the trick, it displays the child pages of the current page, but if there are no child pages, it displays the child pages of the parent page, and a link back to the parent page.

/**
 * @brief: Shows the subpages of the current page, or
 *         the adjacent sibling pages.
 **/
function show_subpages(){

    global $post;
    $subpages = wp_list_pages( array(
        'echo'=>0,
        'title_li'=>'',
        'depth'=>2,
        'child_of'=> ( $post->post_parent == 0 ? $post->ID : $post->post_parent)
    ));
    if ( !empty($subpages) ) {

        if ( $post->post_parent != 0 ) {
            echo '<p class="parent-link"><em>'. __('Back to') .' <a href="'. get_permalink($post->post_parent) .'">'. get_the_title($post->post_parent) .'</em></a><p>';
        }
        echo '<ul>';
        echo $subpages;
        echo '</ul>';
    } else {
        echo 'no subpages';
    }
}

You may need to place this in the main loop

share|improve this answer
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.