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 ?

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' );
  • that's makes sense. I wasn't sure about calling the id when in a loop – pressword Oct 14 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) );
-1

You can get the image thumbnail size using WP functions get_post_thumbnail_id() and wp_get_attachment_image_src()

For example:

$thumbnail_id = get_post_thumbnail_id( $post_id );
$image_attributes = wp_get_attachment_image_src( $thumbnail_id );
$width =  $image_attributes[1];
$height = $image_attributes[2];
  • I want to display the thumbnail sized image. not the dimensions for that image. Sorry for the confusion. – pressword Oct 11 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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