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 the following function that lists all Child-Pages of a page.

function show_subpages() {

    global $post;
    $subpages = wp_list_pages( array(
        'echo'          =>0,
        'title_li'      =>'',
        'depth'         =>2,
        'link_before'   => '— ',
        'child_of'      => ( $post->post_parent == 0 ? $post->ID : $post->post_parent)
    ));
    if ( !empty($subpages) ) {
        echo '<ul id="subpages" class="wrapper">';
        echo $subpages;
        echo '</ul>';
    }
}

So when I'm on the parent-page I see a list with all the child-pages. If I'm on a child-page I see the same list with all the child-pages corresponding with the parent-page.

— My Parent Page
   — My Child Page Nr. 1
   — My Child Page Nr. 2
   — My Child Page Nr. 3

This is almost what I want! The only little thing I need to add is to remove the "current" page I'm on from the list of child-pages.

So imagine I'm on My Child Page Nr. 2 I only want to list My Child Page Nr. 1 and My Child Page Nr. 3 but not "Nr. 2" since I'm currently on this one.

Any ideas how to add this?

Thank you in advance.

share|improve this question

1 Answer 1

up vote 1 down vote accepted
function show_subpages() {

    global $post;
    $subpages = wp_list_pages( array(
        'echo'          =>0,
        'title_li'      =>'',
        'depth'         =>2,
        'link_before'   => '&mdash; ',
        'child_of'      => ( $post->post_parent == 0 ? $post->ID : $post->post_parent),
        'exclude'       => ( $post->post_parent == 0 ? '' : $post->ID)
    ));
    if ( !empty($subpages) ) {
        echo '<ul id="subpages" class="wrapper">';
        echo $subpages;
        echo '</ul>';
    }
}

wp_list_pages supports an exclude parameter. In the above, I've simply added 'exclude' => ( $post->post_parent == 0 ? '' : $post->ID) to the parameter array.
It works analogous to the way you are already feeding values to child_of:
If the post does not have a parent, we feed exclude an empty string. If it has one, we feed it the current post ID.

share|improve this answer

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.