Last active
December 29, 2015 11:48
-
-
Save rubenCodeforges/7665808 to your computer and use it in GitHub Desktop.
Latest post function for wordpress with short code
This file contains 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
function get_latest_posts ($args) { | |
// change "no-cat" to your default "nocategory" catSlug | |
$blogCat = !empty($args['cat'])?$args['cat']:"no-cat"; | |
$postsCount = !empty($args['count'])?$args['count']:"2"; | |
$html; | |
$postArgs = array( | |
'posts_per_page' => $postsCount, | |
'category_name' =>$blogCat | |
); | |
$newQuery = new WP_Query($postArgs); | |
//Mini Loop | |
if($newQuery->have_posts()): | |
while($newQuery->have_posts()):$newQuery->the_post(); | |
// Wrap each post | |
$html.='<div class="latest-post">'; | |
// The thumbnail has some attributes look at the codex http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail | |
$html.=get_the_post_thumbnail(); | |
// First get the title | |
$html.=get_the_title(); | |
// Second the Ecxerpt | |
$html.=get_the_excerpt(); | |
// Now the more link | |
$html.='<a href="'.get_permalink().'">read more</a>'; | |
//Close wrapper | |
$html.="</div>"; | |
endwhile; | |
endif; | |
wp_reset_postdata(); | |
return $html; | |
} | |
add_shortcode('latest_posts','get_latest_posts'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment