Add a pages featured image to a foreach loop

2019-03-03 18:10发布

问题:

I've got a foreach loop that is displaying a list of subpages Title, Content and URL. I can't seem to display the featured image though! Here's what I have:

<?php
$pageChildren = get_pages('sort_column=menu_order&hierarchical=0&child_of='.$post->ID);
if ( $pageChildren ) {
foreach ( $pageChildren as $pageChild ) {
echo '<div style="position:relative;float:left;width:100%;margin:0 0 20px 0;border-bottom:1px dashed #cdcdcd;padding:0 0 20px 0">';
//FEATURED IMAGE HERE
echo '<a class="newstitle" href="' . get_permalink($pageChild->ID) . '">' . $pageChild->post_title.'</a><br /><br />';
if (!empty($pageChild->post_content)){
        echo '<p>'.$pageChild->post_content.'</p> ';
        echo '<a class="readmore" href="' . get_permalink($pageChild->ID) . '" style="float:left">Read More &raquo;</a>';
    }
    echo '</div>';
}
}
?>

回答1:

you must use

<?php echo get_the_post_thumbnail($pageChild->ID ); ?> 

to get the post thumbnail as well page