Skip to content

Instantly share code, notes, and snippets.

@cmg-jess
Created July 27, 2015 23:29
Show Gist options
  • Save cmg-jess/cb9196af26e8e865df8a to your computer and use it in GitHub Desktop.
Save cmg-jess/cb9196af26e8e865df8a to your computer and use it in GitHub Desktop.
<?php
$args = array(
'numberposts' => 2,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' =>' post' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<div class="recent-post">';
$post_id = $recent["ID"];
$post_object = get_post( $post_id );
$post_content = apply_filters('the_content', $post_object->post_content );
$trimmed_content = wp_trim_words( $post_content, 40, '...' );
echo '<h3 class="recent-post-title"><a href="' . get_permalink($post_id) . '">' . $post_object->post_title . '</a></h3>';
if ( has_post_thumbnail( $post_id ) ) :
$image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ) );
$image = $image_array[0];
else :
$image = get_template_directory_uri() . '/images/placeholder-featured-img.jpg';
endif;
echo '<img src="' . $image .'" class="recent-post-img" />';
echo '<p class="recent-post-blurb">' . $trimmed_content . '</p>';
echo '<a href="' . get_permalink($post_id) . '" class="recent-post-link">Read More</a>';
echo '</div>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment