Home / functions.php / Unlimited number of child pages using wp_list_pages
  • How to Unlimited number of child pages using wp_list_pages

    unlimited-number-of-child-pages-using-wp_list_pages

    Adding this snippet to the sidebar.php of your wordpress theme will use wp_list_pages to display all child pages regardless of depth. This is something I have not found online and needed for a current theme I’m working on, enjoy!

    ( example screenshot ) ( click code to copy )

    wordpress snippet : PHP

    <>
    	if ($post->post_parent) {
    	        $ancestors=get_post_ancestors($post->ID);
    	        $root=count($ancestors)-1;
    	        $parent = $ancestors[$root];
    	} else {
    	        $parent = $post->ID;
    	}
    	$children = get_pages('child_of='.$parent);
    	$child_pages = array(1);
    	foreach($children as $child) {
    		array_push($child_pages,$child->ID);
    	}
    	$all_pages = implode(",",$child_pages);
    	if( count( $children ) != 0 ) {
    	echo '<ul class="sidebar-navigation">'.
    		wp_list_pages( 'title_li=&sort_column=menu_order&echo=0&include='.$all_pages )
    	    .'</ul>';
    	}
    

    ( WordPress codex functions, hooks, in this snippet. )

    get_pages, wp_list_pages, menu_order, get_post, get_post_ancestors, get_page, wp,