How do I modify this page template to show subpage excerpts (not post excerpts)?

I have made a page template which shows linked subpages using this bit of code:

<?php
        $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_or

        foreach( $mypages as $page ) {
                $content = $page->post_content;
                if ( ! $content ) // Check for empty page
                        continue;

                $content = apply_filters( 'the_content', $content );
?>
        <h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a><
        <div class="entry"><?php echo $content; ?></div>
<?php
}
?>

I got the code from here and have changed it to order subpages in the order of their Page Order.

Read More

It works, but it displays the full pages’ contents. I only want it to show page excerpts, with the featured image.

(I have enabled page excerpts in the editor by adding add_post_type_support( 'page', 'excerpt' ); to functions.php)

How do I modify this page template to show subpage excerpts?

Related posts

Leave a Reply

2 comments

  1. Slightly modify these lines

    $content = $page->post_content;
    if ( ! $content ) // Check for empty page
        continue;
    
    $content = apply_filters( 'the_content', $content );
    

    to

    $content = $page->post_excerpt;
    if ( ! $content ) // Check for empty excerpt content & fallback to full content
        $content = $page->post_content;
    if ( ! $content ) // Check for empty page
        continue;
    
    $content = apply_filters( 'the_excerpt', $content );
    

    UPDATE:
    for adding featured image use

    <div class="image"><?php echo get_the_post_thumbnail($page->ID);?></div>
    
  2. A reworked example of your snippet

    This sample shows you how you can make use of plugins that hook their actions into filters or hooks. It also allows to use password protection for the child post incl. default core translation strings.

    $wpse69264_child_pages = get_pages( array(
         'child_of'    => in_the_loop ? get_the_ID() : $post->ID // Are we in the loop?
        ,'sort_column' => 'menu_order'
        ,'sort_order'  => DESC
    ) );
    
    foreach ( $wpse69264_child_pages as $child )
    {
        // Default filters: Excerpt length - Also takes languages in account where 1 char = 1 word (like Chinese)
        $output = $child->post_excerpt ? $child->post_excerpt : apply_filters( 'excerpt_length', $child->post_content );
    
        if ( post_password_required( $child ) )
        {
            // Default translation
            _e( 'There is no excerpt because this is a protected post.' );
            continue;
        }
    
        // Just in case    
        $output = force_balance_tags( $output );
    
        // Default filters:
        $output = apply_filters( 'get_the_excerpt', $output );
        echo apply_filters( 'the_excerpt', $output );
    }
    unset( $wpse69264_child_pages, $child );