1

I have created a shortcode with this code:

add_shortcode('recent-newsletter', 'recent_newsletter');
function recent_newsletter() {
    $recentnews = new WP_Query(array(
        'post_type' => 'newsletter',
        'posts_per_page' => 1
    ));
    $news ='';
    if ($recentnews->have_posts()) {
        while($recentnews->have_posts()){
            $recentnews->the_post();
         if (has_post_thumbnail() ) {
             $news .= get_the_post_thumbnail('thumbnail');
            }
         $news .= '<a href="'.get_permalink().'">'.get_the_title().'</a>';
        }
    } else { $news = 'no posts';}
    wp_reset_postdata();
return $news;
}

First off the CPT is working fine and images show on the main query.

The problem I'm having is that I want to pull the thumbnail size, not the full image.

this line:

         $news .= get_the_post_thumbnail('thumbnail');

displays no image. If I change it to this:

         $news .= get_the_post_thumbnail();

The full image shows up. How do I display just the thumbnail sized-image ?

2 Answers 2

3

The first argument of get_the_post_thumbnail() is not the image size that you want to use. It's the ID or object of the post whose thumbnail you want to get. To get the thumbnail size, set the second argument to 'thumbnail'. The first argument can just be set to null to get it for the current post:

$news .= get_the_post_thumbnail( null, 'thumbnail' );
1
  • that's makes sense. I wasn't sure about calling the id when in a loop
    – pressword
    Oct 14, 2019 at 14:58
2

You should pass first argument as post id in get_the_post_thumbnail(). As you're getting thumbnail if look you can pass the id of post like

get_the_post_thumbnail( get_the_ID(), 'thumbnail' );

you can also specify the size of the image your want to show as

get_the_post_thumbnail( get_the_ID(), array( 100, 100) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.