How to get all child pages of a parent page in wordpress?
Here you go. A bit late for the author, but people will come here for an answer still ;-)
<?php // determine parent of current pageif ($post->post_parent) { $ancestors = get_post_ancestors($post->ID); $parent = $ancestors[count($ancestors) - 1];} else { $parent = $post->ID;}$children = wp_list_pages("title_li=&child_of=" . $parent . "&echo=0");if ($children) {?> <ul class="subnav"> <?php // current child will have class 'current_page_item' echo $children; ?> </ul><?php } ?>
The easiest way to handle this is with get_children()
. It pretty much does what you would expect. It returns the child pages of a parent page.
get_children()
is basicly a wrapper for the WP_Query
class.
You can use it like this...
$child_args = array( 'post_parent' => 1, // The parent id. 'post_type' => 'page', 'post_status' => 'publish');$children = get_children( $child_args );
If you want to return the children of the current post you can pass in $post->ID
as the 'post_parent'
.