The correct way to get recent posts with thumbnails in WordPress without plugin, shortcode or widget, thumbnails full size or custom size.

How to get Recent Posts in WordPress

Using wp_get_recent_posts() function you can get recent posts, and you can get recent posts using get_posts() function, but we will use wp_get_recent_posts() function.

How to get Recent Posts with Thumbnails

You need wp_get_recent_posts() or get_posts() function, with:

Get Recent Posts with Thumbnails

This is an example to get recent posts with thumbnails, copy the code and paste it in your “functions.php” file in your theme:

function wptime_get_recent_posts($post_id = null, $count = null){
    // Written by Qassim Hassan | wp-time.com

    if( !empty($post_id) ){
        $id = $post_id;
    }else{
        $id = '';
    }

    if( empty($count) ){
        $count = 5; // recent posts count is 5, default, you can change the number.
    }

    $args = array(
                'numberposts' => $count,
                'offset' => 0,
                'category' => 0, // you can get recent posts by category id
                'orderby' => 'post_date',
                'order' => 'DESC',
                'include' => '',
                'exclude' => $id, // exclude some post, for example exclude current post by current post ID: 'exclude' => 123, or 'exclude' => array(123,456)
                'meta_key' => '',
                'meta_value' =>'',
                'post_type' => 'post', // posts only, you can change it, if you want recent pages use "page", or custom post type for example: "movies"
                'post_status' => 'publish', // published posts only, you can change it: draft, future, pending, private
                'suppress_filters' => true
            );

    $recent_posts = wp_get_recent_posts($args, false);

    ob_start();

    foreach ($recent_posts as $recent_post) {
        $post_link = esc_url( get_permalink($recent_post->ID) );
        $post_title_attr = esc_attr( get_the_title($recent_post->ID) );
        $post_title = get_the_title($recent_post->ID);

        if( has_post_thumbnail($recent_post->ID) ){ // check if the post has thumbnail
            $thumbnail_id = get_post_thumbnail_id($recent_post->ID);
            $image_size = 'full'; // image size is full, you can change the size, enter: "large" or "thumbnail"
            $get_thumbnail_url = wp_get_attachment_image_src($thumbnail_id, $image_size);
            $image = '<img src="'.$get_thumbnail_url[0].'" alt="'.$post_title_attr.'">'; // alt is optional
        }else{
            $image = '<img src="http://example.com/default-image.png" alt="'.$post_title_attr.'">'; // if not has thumbnail will be display default image, change image link to your default image link.
            //$image = null; // use this variable if you don't want to display default image.
        }
        ?>
            <div class="recent-post">
                <?php echo $image; ?>
                <a href="<?php echo $post_link; ?>" title="<?php echo $post_title_attr; ?>"><?php echo $post_title; ?></a>
            </div>
        <?php
    }

    $get_recent_posts = ob_get_clean();

    echo $get_recent_posts;
}

Now we will create Shortcode for recent posts with thumbnails, copy the code and paste it in your “functions.php” file:

function wptime_recent_posts_shortcode($atts){
    ob_start();

    global $post;

    $post_id = $post->ID; // get current post id to exclude it from recent posts

    if( !empty($atts['count']) ){
        $count = $atts['count']; // recent posts count, from shortcode, for example: [wptime_recent_posts count="3"]
    }else{
        $count = null;
    }

    wptime_get_recent_posts($post_id, $count);

    return ob_get_clean();
}
add_shortcode('wptime_recent_posts', 'wptime_recent_posts_shortcode');

Now use this shortcode in your post to display recent posts:

Use: [wptime_recent_posts] (default is 5 recent posts)
Or use: [wptime_recent_posts count="3"] (display 3 recent posts, you can change the number)

If you want to display recent posts after all posts automatically read this tutorial.

Anyway you can display recent posts anywhere inside your theme:

<?php

    // Anywhere
    wptime_get_recent_posts(null, 3); // display 3 recent posts, you can change the number
    // or:
    echo do_shortcode('[wptime_recent_posts]');

    // Inside your WordPress theme loop:
    wptime_get_recent_posts($post->ID, 6); // display 6 recent posts and exclude current post, you can change the number

?>

Recent Posts Widget

Use “[wptime_recent_posts]” in WordPress text widget:

recent posts with thumbnails widget

If the shortcode not working in text widget read this tutorial about display shortcode in text widget, or read tutorial about how to create a custom WordPress widget.