List Current Parent and Child Pages in WordPress

November 22nd, 2011

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');

2 Responses to "List Current Parent and Child Pages in WordPress"

  1. Nete says:

    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?

  2. CoryMathews says:

    @Nete I added a bit to the end of the article, which should help you out.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>