Recent Excerpts Shortcode - Display recent posts excerpts by using [recent_excerpts count=3 length=20 cat="FRONT"]
// Recent Excerpts
function recentexcerpts_shortcode($atts) {
global $a; //Make attributes accessible by child functions
// Default attributes
$a = shortcode_atts(array(
'count' => 5,
'length' => 55,
'cat' => null,
'showdate' => true,
'dateformat'=> 'j M Y'
), $atts);
//Set custom excerpt length
function custom_excerpt_length($length) { global $a; return $a['length']; }
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
// Populate arguments for query
$q = new WP_Query(array(
'posts_per_page' => $a['count'],
'cat' => $a['cat']
));
// If posts are found...
if ($q->have_posts()) {
//Display before the loop
$d = "<ul>\n";
//The loop
while ($q->have_posts()) { $q->the_post();
$d .= "<li class=\"cat-post-item\">\n";
$d .= "<a class=\"post-title\" rel=\"bookmark\" href=\"". get_the_permalink() ."\" title=\"" . get_the_title() . "\">" . get_the_title() . "</a>\n";
if ($a['showdate'] == true) { $d .= "<p class=\"post-date\">" . get_the_date($a['dateformat']) . "</p>\n"; };
$d .= "<p>" . get_the_excerpt() . "</p>\n";
$d .= "</li>\n";
}
//Display after the loop
$d .= "</ul>\n";
wp_reset_postdata(); //Reset postdata
} else { $d = "<p>No posts at the moment.</p>\n"; }; // Display this if no posts are found
remove_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); // Remove custom excerpt length filter
return $d; // Return the results
}
add_shortcode('recent_excerpts', 'recentexcerpts_shortcode'); // Register shortcode
add_filter('widget_text', 'do_shortcode'); // Make widgets do shortcodes