Skip to content

Instantly share code, notes, and snippets.

@gstricklind
Last active August 29, 2015 14:02
Show Gist options
  • Save gstricklind/ff4603a0ef8d0d476ecf to your computer and use it in GitHub Desktop.
Save gstricklind/ff4603a0ef8d0d476ecf to your computer and use it in GitHub Desktop.
WordPress: Add number of post excerpts via shortcode
<?php
/**
* Excerpts Shortcode
*/
//[excerpts display="3"]
function kedc_shortcode_excerpts($atts) {
extract( shortcode_atts( array(
'display' => ''
), $atts) );
$query_posts = new WP_Query('posts_per_page=' . $display);
$list = '<ul class="excerpts">';
while($query_posts->have_posts()) : $query_posts->the_post();
$list .= '<li>' . '<h4 class="excerpt-header"><a href="'. get_permalink() . '">' . get_the_title() . '</a></h4>' . '<p class="excerpt-date">' . get_the_date() . '</p>' . '<p class="excerpt-content">' . get_the_excerpt() . '</p>' . '<a class="read-more" href="'. get_permalink() . '"> Continue reading &#8594;</a>' . '</li>';
endwhile;
wp_reset_query();
return $list . '</ul>';
}
add_shortcode('excerpts', 'kedc_shortcode_excerpts');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment