How to display list of child pages of parent page in wordpress?

Add this code to your functions.php. An explanation of the code is given below.

function wpb_list_child_pages() { 

    global $post; 

    $id = ( is_page() && $post->post_parent ) ? $post->post_parent : $post->ID;
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=" . $id . "&echo=0' );
    //you can add `&depth=1` in the end, so it only shows one level

    if ( $childpages ) {    
        $string = '<ul>' . $childpages . '</ul>';
    }

    return $string;
}

add_shortcode('wpb_childpages', 'wpb_list_child_pages');

Explanation

The code checks to see if a page has a parent or the page itself is a parent. If it is a parent page, then it displays the child pages associated with it. If it is a child page, then it displays all other child pages of its parent page. Lastly, if this is just a page with no child or parent page, then the code will simply do nothing. So just add this shortcode [wpb_childpages] to the page where it’s child pages will be displayed.

My test output that worked at my localhost:

Test1

-t1

–tt1

-t2

and the output that displays when I wrote that shortcode in Test1 page is:

t1

t2

Leave a Comment