Skip to content

Instantly share code, notes, and snippets.

@alisonmf
Last active October 28, 2022 18:32
Show Gist options
  • Save alisonmf/6625197 to your computer and use it in GitHub Desktop.
Save alisonmf/6625197 to your computer and use it in GitHub Desktop.
WordPress - Get child pages and grandchild page list. Get ancestor list when on grandchild page.
<?php
if(is_page())
{
//Assuming current working page is the parent
//
$the_parent_id = $post->ID;
//Otherwise get the greatest ancestor id
//
if(! empty($post->ancestors))
{
$the_parent_key = max(array_keys($post->ancestors));
$the_parent_id = $post->ancestors[$the_parent_key];
}
//First, get all of the pages
//
$all_wp_pages = get_pages();
//Get all of the children relative to the greatest ancestor
//
$page_services_children = get_page_children($the_parent_id, $all_wp_pages);
$page_walk_defaults = array();
$page_walk_defaults['depth'] = 3;
$page_walk_defaults['show_date'] = '';
$page_walk_defaults['date_format'] = get_option('date_format');
$page_walk_defaults['child_of'] = 0;
$page_walk_defaults['exclude'] = '';
$page_walk_defaults['title_li'] = '';
$page_walk_defaults['echo'] = 0;
$page_walk_defaults['authors'] = '';
$page_walk_defaults['sort_column'] = 'post_date';
$page_walk_defaults['link_before'] = '';
$page_walk_defaults['link_after'] = '';
$page_walk_defaults['walker'] = '';
$output = '';
$output .= '<ul>';
$output .= '<li>'.get_the_title($the_parent_id).'</li>';
$output .= walk_page_tree($page_services_children, $page_walk_defaults['depth'], $the_parent_id, $page_walk_defaults);
$output .= '</ul>';
$output = apply_filters('wp_list_pages', $output, $page_walk_defaults);
echo $output;
}
?>
@trishah
Copy link

trishah commented Oct 9, 2018

I'm also looking to add the current_page_item to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment