Recent Posts With Thumbnails Shortcode
Shortcode [recent_posts_thumbs] that outputs a configurable list of recent posts with featured image thumbnails and dates.
PHPby SnipCraft
php
<?php
if ( ! function_exists( 'scseed_recent_posts_thumbs_shortcode' ) ) {
/**
* Usage: [recent_posts_thumbs count="5" thumb_size="thumbnail" show_date="yes"]
*/
function scseed_recent_posts_thumbs_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'count' => 5,
'thumb_size' => 'thumbnail',
'show_date' => 'yes',
),
$atts,
'recent_posts_thumbs'
);
$query = new WP_Query( array(
'posts_per_page' => absint( $atts['count'] ),
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
) );
if ( ! $query->have_posts() ) {
return '';
}
$thumb_size = sanitize_key( $atts['thumb_size'] );
$show_date = ( 'yes' === $atts['show_date'] );
ob_start();
echo '<ul class="scseed-recent-posts">';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li class="scseed-recent-post">';
if ( has_post_thumbnail() ) {
echo '<a href="' . esc_url( get_permalink() ) . '" class="scseed-rp-thumb" tabindex="-1" aria-hidden="true">';
the_post_thumbnail( $thumb_size );
echo '</a>';
}
echo '<div class="scseed-rp-content">';
echo '<a class="scseed-rp-title" href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a>';
if ( $show_date ) {
echo '<time class="scseed-rp-date" datetime="' . esc_attr( get_the_date( 'c' ) ) . '">' . esc_html( get_the_date() ) . '</time>';
}
echo '</div>';
echo '</li>';
}
echo '</ul>';
wp_reset_postdata();
return ob_get_clean();
}
}
add_shortcode( 'recent_posts_thumbs', 'scseed_recent_posts_thumbs_shortcode' );#posts#recent-posts#shortcode#thumbnails#widgets