I would tend not to go with wp_get_recent_posts
or even get_posts
for custom queries like this. By default, template tags like the_excerpt()
is not available to these functions, and you have to make use of setup_postdata($post)
to have access to these template tags.
I would personally use WP_Query
in a case like this which is more flexible, specially when have a custom query that needs to be paginated.
Here is an example of a custom query to get the newest 5 posts. Note: I have not included any html mark-up. For a list of all available arguments, check out the link I’ve provided in WP_Query
$args = array(
'posts_per_page' => 5,
'order' => 'DESC'
);
$rp = new WP_Query( $args );
if($rp->have_posts()) :
while($rp->have_posts()) : $rp->the_post();
the_title(); // posttitle
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(); //display the thumbnail
}
the_excerpt(); // displays the excerpt
endwhile;
wp_reset_postdata(); // always always remember to reset postdata when using a custom query, very important
endif;
For further reading:
-
The Loop (Just a note here, certain examples uses
query_posts
, which should never be used. Rather useWP_Query
to construct custom queries)