the_post_thumbnail no longer returning an img tag with srcset
-
I updated my staging env’t to the latest RICG plugin (3.0.0) and noticed that for a custom template I’m using, I’m no longer getting srcset and sizes added automatically for an image.
The template has:
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}Prior to the update, this was getting the srcset and sizes attributes added. Can I no longer use the_post_thumbnail() and expect back an <img> tag with those attributes set for me?
Thanks,
Sheelah
-
Hi Sheelah,
An image element generated by
the_post_thumbnail()
may have asrcset
andsizes
attribute added, depending on your theme settings. If your theme uses a custom size hard crop for the post thumbnail and there are no other sizes matching the same aspect ratio, then nosrcset
will get added. Is it possible that this is what is happening in your case?Joe
Hi Joe-
No, there are no hard crop sizes set for my theme.
It does work as it did in the previous version (generating srcset and sizes) if I remove the call to the_post_thumbnail() and use this instead, but it’s a lot more code:
if ( has_post_thumbnail() ) {
$args = array(
‘sizes’ => array(
array(
‘size_value’ => ‘100vw’
),
)
);$post_thumb = get_post_thumbnail_id( get_the_ID() );
$sizes = tevkori_get_sizes( $post_thumb, ‘medium’, $args );
echo wp_get_attachment_image( $post_thumb, ‘medium’, false, array(‘sizes’ => $sizes, ‘class’ =>’attachment-post-thumbnail’) );Thanks for any other ideas.
I’m having the same issue.
the_post_thumbnail() is returning a standard img tag with only the standard width, height, src, class, and alt attributes.
same here.
<figure><?php the_post_thumbnail( 'large' ); ?></figure>
is returning
<img width="640" height="364" src="http://www.****.jpg" class="attachment-large wp-post-image" alt="***">
Thanks everyone.
What I think may be happening here is that,
the_post_thumbnail()
by default passes the named image sizepost-thumbnail
toget_the_post_thumbnail()
. If your theme doesn’t have apost-thumbnail
size defined, it will fall back to the full size, but thesrcset
functionality doens’t fall back in the same way.To test if this is the case, try passing the size ‘medium’
to the_post_thumbnail()
and see if the resulting markup containssrcset
andsizes
attributes. Additionally, if you pass a size like ‘large’ and the original image isn’t big enough to create a large size, the result would be the same.We’ll look into adjusting this behavior so the fallbacks are consistent.
Hi Joe-
Thanks for looking into this. That does seem to work. Using the_post_thumbnail(‘full’) returns the img tag with srcset and sizes attributes. However, I am wondering why it’s not including the original size image (full) in the srcset though as it did in the previous plugin version. It’s setting it only in the src attribute, but without it in srcset it’s never actually available for use.
Now I get:
<img width=”2048″ height=”1316″ src=”http://local.dev/wp-content/uploads/2015/11/flower.jpg” class=”attachment-full wp-post-image” alt=”flower” srcset=”http://local.dev/wp-content/uploads/2015/11/flower-300×193.jpg 300w, http://local.dev/wp-content/uploads/2015/11/flower-1024×658.jpg 1024w, http://local.dev/wp-content/uploads/2015/11/flower-1450×932.jpg 1450w” sizes=”(max-width: 2048px) 100vw, 2048px” />Last version of the plugin returned this when my template used the_post_thumbnail():
<img width=”2048″ height=”1316″ src=”http://local.dev/wp-content/uploads/2015/11/flower.jpg” class=”attachment-post-thumbnail wp-post-image” alt=”flower” srcset=”http://local.dev/wp-content/uploads/2015/11/flower-300×193.jpg 300w, http://local.dev/wp-content/uploads/2015/11/flower-1024×658.jpg 1024w, http://local.dev/wp-content/uploads/2015/11/flower-1450×932.jpg 1450w, http://local.dev/wp-content/uploads/2015/11/flower.jpg 2048w” sizes=”(max-width: 2048px) 100vw, 2048px” />I tried dequeuing Picturefill and using the previous version of it (2.3.1) but got the same result.
Never mind.. I discovered the max_srcset_image_width filter added in this release of the plugin (in the https://wordpress.org/support/topic/firefox-not-loading-in-larger-images-on-resize thread) so I was able to override the max image width default of 1600px.
I’ve now been able to reproduce my img with srcset and sizes set as they were in the previous release, using the_post_thumbnail(size) and the max_srcset_image_width filter.
Thanks for confirming Sheelah.
I’m working on a fix for the issue with
the_post_thumbnail()
. You can follow the progress here: https://github.com/ResponsiveImagesCG/wp-tevko-responsive-images/issues/232.Joe
This issue only seem to occur when there is no size defined for the post thumbnail with
set_post_thumbnail_size()
. The problem has been resolved by some changes we made after 3.0.0. We will release a new version within a few days.Sheelah, Can you confirm that your theme doesn’t set a post thumbnail size? I.e. the featured image is the full size image.
Agitana, Your problem seem to be a different case because you specify image size “large” when you call
the_post_thumbnail()
. Maybe you can provide a bit more information about your case. What is the original size of the image and what dimensions do you use for image sizes “thumbnail” (crop or no crop), “medium”, and “large”? Are any additional image sizes added withadd_image_size()
?Thanks!
Hi Jasper-
That’s right.. My theme doesn’t have a post thumbnail size set.
Agitana, Your problem seem to be a different case because you specify image size “large” when you call the_post_thumbnail(). Maybe you can provide a bit more information about your case. What is the original size of the image and what dimensions do you use for image sizes “thumbnail” (crop or no crop), “medium”, and “large”? Are any additional image sizes added with add_image_size()?
Thanks for your answers so far, Jasper.
I guess i found the problem for the issue: the W3 Total Cache Plugin.I ran my theme on a localhost without W3 Total Cache and everything works fine. Then i ran it on an online testsite (with W3 Total Cache but without CDN) and it worked on some images (strange?!) and on our mainsite (with W3 Total Cache and with CDN) it doesnt work at all.
- The topic ‘the_post_thumbnail no longer returning an img tag with srcset’ is closed to new replies.