// Plenty of Fortune: Latest Posts shortcode for Classic Editor
function pof_latest_posts_shortcode($atts) {
$atts = shortcode_atts(array(
‘posts’ => 6,
‘show_image’ => ‘yes’,
‘show_excerpt’ => ‘yes’,
‘excerpt_words’=> 22,
), $atts, ‘pof_latest_posts’);
$posts_per_page = max(1, intval($atts[‘posts’]));
$show_image = ($atts[‘show_image’] === ‘yes’);
$show_excerpt = ($atts[‘show_excerpt’] === ‘yes’);
$excerpt_words = max(5, intval($atts[‘excerpt_words’]));
$q = new WP_Query(array(
‘post_type’ => ‘post’,
‘posts_per_page’ => $posts_per_page,
‘post_status’ => ‘publish’,
‘ignore_sticky_posts’ => true,
));
if (!$q->have_posts()) {
return ‘
No posts yet. Check back soon.
‘;
}
ob_start();
echo ‘
while ($q->have_posts()) {
$q->the_post();
echo ‘
if ($show_image && has_post_thumbnail()) {
echo ‘‘;
the_post_thumbnail(‘medium’);
echo ‘‘;
}
echo ‘
‘ . esc_html(get_the_title()) . ‘
‘;
echo ‘
‘;
if ($show_excerpt) {
$text = wp_strip_all_tags(get_the_excerpt());
$words = preg_split(‘/\s+/’, trim($text));
if (count($words) > $excerpt_words) {
$text = implode(‘ ‘, array_slice($words, 0, $excerpt_words)) . ‘…’;
}
echo ‘
‘ . esc_html($text) . ‘
‘;
}
echo ‘
‘;
}
echo ‘
‘;
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode(‘pof_latest_posts’, ‘pof_latest_posts_shortcode’);