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.
wp_list_pages
supports anexclude
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.