1

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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