I have this simple shortcode to display the last posts with it’s title and except:
add_shortcode('latest3', function(){
$recent_posts = wp_get_recent_posts(
array(
'numberposts' => 3,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
), ARRAY_A);
$output="<h2>Latest posts</h2>";
foreach ( $recent_posts as $recent ) {
$output .= '<h3>'.$recent["post_title"].'</h3>';
$output .= $recent["post_excerpt"];
}
return $output;
});
But for some reason the excerpt output is empty. print_r($recents)
shows that there is indeed an array key called post_excerpt
but is shown always empty.
2 Answers
The post_excerpt
value is empty because you have no explicit excerpts for your posts. While the_excerpt()
does generate an excerpt from the posts content if the post excerpt is empty, the function wp_get_recent_posts()
, which is basically a wrapper for get_posts()
, doesn’t.