Skip to main content

Display child pages featured image, page title & content on parent page WordPress.

Display child pages featured image, page title & content on parent page WordPress.

In this article we will learn about display child page content on parent page. Below code you will loop corrent page all child page featured image, page title and content on your parent page.

First open function.php and paste below code on your php file.

function show_childpages_shortcode() {
    // a shortcode should just return the content not echo html
    // so we start to create an object, and on the end we return it
    // if we dont do this the shortcode will be displayed in the top of the content
    ob_start();

    // only start if we are on a single page
    if ( is_page() ) {
        // get the ID of the current (parent) page
        $current_page_id = get_the_ID();
        // get all the children of the current page
        $child_pages = get_pages( array( 
            'child_of' => $current_page_id,  
        ) );
        // start only if we have some childpages
        if ($child_pages) {
            // if we have some children, display a list wrapper
            echo '<div class="row windows_row_holder">';
            // loop trough each childpage 
            foreach ($child_pages as $child_page) {

                $page_id    = $child_page->ID; // get the ID of the childpage
                $page_link  = get_permalink( $page_id ); // returns the link to childpage
                $page_img   = get_the_post_thumbnail( $page_id, 'full' ); // returns the featured image <img> element
                $page_title = $child_page->post_title; // returns the title of the child page
        $page_content = $child_page->post_content; // returns the title of the child page

                ?>
            <div class="childpage-<?php echo $page_id; ?> col-md-4 pb_30">
                <div class="inner">
                    <div class="img_holder"><?php echo $page_img; //display featured image ?></div>
                    <div class="content_txt">
                        <h3><?php echo $page_title; ?></h3>
                        <p><?php echo $page_content; ?></p>
                    </div>
                </div>
            </div>
            <?php
            }//END foreach ($child_pages as $child_page)
            echo '</div>';
        }//END if ($child_pages)    
    }//END if (is_page())
    // return the object
    return ob_get_clean();
}
add_shortcode( 'show_childpages', 'show_childpages_shortcode' );

Then create a custom template. Paste these below short code here.

<?php echo do_shortcode('[show_childpages]'); ?>

Below image you will choose your parent custom template.

Display child pages featured image, page title & content on parent page WordPress.

Leave a Reply

avatar
  Subscribe  
Notify of