Created
February 19, 2014 18:28
Recent Posts Shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Add recent posts shortcode. Use like so: [recent-posts posts='5' slug='uncategorized'] | |
*/ | |
function recent_posts_function($atts){ | |
extract(shortcode_atts(array( | |
'posts' => 5, | |
'slug' => 'uncategorized' | |
), $atts)); | |
$return_string = '<ul>'; | |
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts , 'category_name' => $slug)); | |
if (have_posts()) : | |
while (have_posts()) : the_post(); | |
$return_string .= '<h4><a href="'.get_permalink().'">'.get_the_title().'</a></h4>'; | |
$return_string .= '<p>'.get_the_excerpt().'</p>'; | |
endwhile; | |
endif; | |
$return_string .= '</ul>'; | |
wp_reset_query(); | |
return $return_string; | |
} | |
add_shortcode('recent-posts', 'recent_posts_function'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment