List Current Parent and Child Pages in WordPress
Recently in WordPress I needed to create a navigation that would display a page’s parent page and all of that pages children, I am a bit rusty with PHP but here is what I got to work.
<ul>
<?php
if($post->post_parent == 0) {
$pageLinkID = $post->ID;
$title = $post->post_title;
} else {
$pageLinkID = $post->post_parent;
$title = get_the_title($post->post_parent);
}
echo '<li class="parent"><a href="'.get_page_link($pageLinkID).'">'.$title.'</a></li>';
$pages = get_pages('child_of='.$pageLinkID);
foreach ($pages as $p) {
echo '<li><a href="'.get_page_link($p->ID).'">'.$p->post_title.'</a></li>';
}
?>
</ul>
If you need to order the posts by the order value change the
$pages =
Line to
$pages = get_pages('child_of='.$pageLinkID.', 'sort_column' => 'menu_order', 'sort_order' => 'asc');
It works great, just what I need, but I want the list to be sorted by the order no I have assigned to the pages – How do I do that?
@Nete I added a bit to the end of the article, which should help you out.