Add Parent to List of Subpages

If I understand what you want, it is trickier than it should be to get it working.

$ancestors = array();
$ancestors = get_ancestors($post->ID,'page');
$parent = (!empty($ancestors)) ? array_pop($ancestors) : $post->ID;
if (!empty($parent)) {
  $pages = get_pages(array('child_of'=>$parent));
  if (!empty($pages)) {
    $page_ids = array();
    foreach ($pages as $page) {
      $page_ids[] = $page->ID;
    }
    $children = wp_list_pages("include=".$parent.','.implode(',',$page_ids)."&echo=1");
    echo '<ul>'.$children.'</ul>'; 
  }
}

You have to get a list of all of your pages and pass them in the include parameter. The basic technique is listed in the Codex. The above is nearly a copy, in fact.

That will give you current_page_ancestor on all ancestor pages and current_page_parent on the immediate parent only. That should be sufficient.

If you notice I have included the top-level parent in the page list. Your code only does that sometimes. I don’t know if that is by design or not, but I included it. You should be able to add or remove that page to match whatever logic you need.

Leave a Comment