Home / Widgets / Create List of Child Pages
Duplicate Snippet

Embed Snippet on Your Site

Create List of Child Pages

To show a list of Child pages on the Parent Page

Code Preview
php
<?php
function wpb_list_child_pages() {
    global $post;
    // Only get child pages of the current page
    $args = array(
        'post_type'      => 'page',
        'posts_per_page' => -1,  // get all child pages
        'post_parent'    => $post->ID,
        'orderby'        => 'menu_order',
        'order'          => 'ASC'
    );
    $child_pages = new WP_Query($args);
    $output = '';
    if ($child_pages->have_posts()) {
        while ($child_pages->have_posts()) {
            $child_pages->the_post();
            $link = get_permalink();
            $title = get_the_title();
            $excerpt = get_the_excerpt();
            $output .= '<h4>';
            $output .= '<a href="' . esc_url($link) . '">' . esc_html($title) . '</a></h4>';
            $output .= '<p class="child-excerpt">' . esc_html($excerpt) . '</p>';
        }
        wp_reset_postdata();
    }
    return $output;
}
add_shortcode('wpb_childpages', 'wpb_list_child_pages');

Comments

Add a Comment