When I use the functions that get and output the post thumbnail, they will display the requested thumbnail in the size you specify, but if this size doesn’t exist they will display the thumbnail in the original size and force the browser to resize it.
How can I force them to display a placeholder like “no image” in case the requested size doesn’t exist?
Assuming the question is about
wp_get_attachment_image_src
.It could be also about
wp_get_attachment_link
, but, although related, this analysis doesn’t includes it.Got aware of this issue answering this question: How can I view all WP generated thumbnails in Media Manager?.
Refer to it to see a working code regarding the issue on this Question.
And it lead to this WordPress forum topic.
The function wp_get_attachment_image_src( $attachment_id, $size, $icon ) returns an array containing:
If
[3]
is false, theoriginal
image data is returned.Both
wp_get_attachment_image_src
andwp_get_attachment_link
rely in the functionimage_downsize
, inside/wp-includes/media.php
.And that’s where this 4 items array is being returned.
I’m Not sure about this but i know you can use php’s getimagesize() function
something like this:
and i know it’s not an answer to the question directly but you can use a plugin called AJAX Thumbnail Rebuild –
This plugin allows you to re-build the post thumbnails. Useful if you add_image_size() after already having uploaded post thumbnails.
This is how I was able to display a thumbnail only if a requested size exists:
To learn more about
wp_get_attachment_image_src
read the Codex.There’s a plugin that will actually create the image size automatically for you if it doesn’t exist. http://austinmatzko.com/wordpress-plugins/filosofo-custom-image-sizes/
I haven’t needed to use this plugin yet myself. But it’s worth a shot. There is talk about adding similar functionality to WP core, maybe in 3.2 http://core.trac.wordpress.org/ticket/15311
It’s seem’s to be a better way.
Use the global variable $_wp_additional_image_sizes, it’s store all registered image_size.
So if you want to check if an image size name is define :
The best approach is to use WordPress’ built-in “get_intermediate_image_sizes” function to return the full list of image sizes as an array then look through that.
Maybe this will help
from: http://codex.wordpress.org/Function_Reference/has_post_thumbnail
I use something like this to check if the image size exists:
Hope it helps get you going.