Skip to content

Instantly share code, notes, and snippets.

@vanbernaert
Last active December 17, 2018 11:49
Show Gist options
  • Save vanbernaert/710939144018a44a9c25bb1f613b1dea to your computer and use it in GitHub Desktop.
Save vanbernaert/710939144018a44a9c25bb1f613b1dea to your computer and use it in GitHub Desktop.
WordPress: List subpages on a parent page through a shortcode
function wpb_list_child_pages() {
global $post;
if ( is_page() && $post->post_parent )
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
else
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
if ( $childpages ) {
$string = '<ul>' . $childpages . '</ul>';
}
return $string;
}
add_shortcode('page_show_childpages', 'wpb_list_child_pages');
//# Instructions
// Put this in functions.php
// use shortcode [page_show_childpages] on page to list subpages
// if you need it automatically on every page, puts next code in the page template php file:
// <?php wpb_list_child_pages(); ?>
// source: http://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/
@RazvanAndreiLismanu
Copy link

And if i want to order the pages by date?
What should i change?
Thanks!

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