Pravayest Pravayest Pravayest Pravayest - 29 days ago 8
PHP Question

wordpress get recent posts with description

I use Wordpress and I want to get recent posts with thumbnail and description.

So I use wp_get_recent_posts.

<?php $args = array( 'numberposts' => '3' );

$recent_posts = wp_get_recent_posts($args);?>
<ul class="main-slider">
<?php foreach( $recent_posts as $recent ){?>
<li>
<?php echo '<div class="textoverlay">
<a href="' . get_permalink($recent["ID"]) . '"><h1>' . $recent["post_title"].'</h1></a>
<p>'.get_the_excerpt().'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>
</li>
<?php
}?>
</ul>


I can get post title and link, but I can't get description:

<p>'.get_the_excerpt().'</p>

Answer

You have just missing of post_id in description without post_id it can't display anything.

<?php $args = array( 'numberposts' => '3' );

$recent_posts = wp_get_recent_posts($args);?>
<ul class="main-slider">
   <?php foreach( $recent_posts as $recent ){?>
        <li>
            <?php echo '<div class="textoverlay">
                <a href="' . get_permalink($recent["ID"]) . '"><h1>' .  $recent["post_title"].'</h1></a>
                    <p>'.get_the_content($recent["ID"]).'</p>
            </div> ';
            if ( has_post_thumbnail($recent["ID"]) ) {
                echo  get_the_post_thumbnail($recent["ID"],'thumbnail');
            } ?>
        </li>
        <?php
    }?>
 </ul>