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 ?
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 ?
Share Improve this question edited Oct 11, 2019 at 17:12 pressword asked Oct 11, 2019 at 16:39 presswordpressword 3801 gold badge4 silver badges13 bronze badges2 Answers
Reset to default 3The 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' );
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) );