Child Pages List Shortcode
Adds a [child_pages] shortcode that lists the subpages of the current page, so section landing pages always link to everything below them.
Opens WPIDE on
Code
add_shortcode( 'child_pages', function ( $atts ) {
$atts = shortcode_atts( array( 'id' => 0, 'depth' => 1, 'sort' => 'menu_order' ), $atts, 'child_pages' );
$parent = $atts['id'] ? absint( $atts['id'] ) : get_the_ID();
if ( ! $parent ) {
return '';
}
$list = wp_list_pages( array(
'child_of' => $parent,
'depth' => absint( $atts['depth'] ),
'title_li' => '',
'echo' => false,
'sort_column' => in_array( $atts['sort'], array( 'menu_order', 'post_title', 'post_date' ), true ) ? $atts['sort'] . ', post_title' : 'menu_order, post_title',
) );
return $list ? '<ul class="wpide-child-pages">' . $list . '</ul>' : '';
} );
Notes
Use [child_pages id="12" depth="2"] to list the children of another page or to include grandchildren.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.