Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
Are you talking about registered thumbnail sizes or you are trying to get arbitrary non-registered size in pixels? Code example please? –  Rarst Jan 27 '11 at 8:31
    
about the registered sizes –  Alex Jan 29 '11 at 9:30

6 Answers 6

I'm Not sure about this but i know you can use php's getimagesize() function something like this:

//This must be in one loop

if(has_post_thumbnail()) {
    $wanted_width = 300;
    $wanted_height = 150;
    $id = get_post_thumbnail_id()
    $image = wp_get_attachment_image_src($id);
    if ($image){
        list($width, $height) = getimagesize($image[0]);
        if (($wanted_height = $width) && ($wanted_height = $height)){
            the_post_thumbnail();
        }
        else{
            //default when no image
            echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />';
        }
    }
    else{
        //default when no image
        echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />';
    }
} else {
    //default when no image
    echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />';
}

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.

share|improve this answer

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

share|improve this answer

Maybe this will help

<?php 
//This must be in a loop
if(has_post_thumbnail()) {
    the_post_thumbnail();
} else {
    echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />';
}
?>

from: http://codex.wordpress.org/Function_Reference/has_post_thumbnail
I use something like this to check if the image size exists:

if (function_exists('has_post_thumbnail') && has_post_thumbnail('medium')) {
//// code to display thumbnail goes here
}
else {
/// otherwise do this
}

Hope it helps get you going.

share|improve this answer

This is how I was able to display a thumbnail only if a requested size exists:

if ( has_post_thumbnail() )  {
    $imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail' ); //change thumbnail to whatever size you are using
    $imgwidth = $imgdata[1]; // thumbnail's width                   
    $wanted_width = 300; //change this to your liking
    if ( ($imgwidth < $wanted_width ) ) {
        the_post_thumbnail();
    } else { 
        //do whatever you want
    }
}

To learn more about wp_get_attachment_image_src read the Codex.

share|improve this answer

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:

[0] => url  
[1] => width  
[2] => height  
[3] => is intermediate

If [3] is false, the original image data is returned.


Both wp_get_attachment_image_src and wp_get_attachment_link rely in the function image_downsize, inside /wp-includes/media.php.
And that's where this 4 items array is being returned.

share|improve this answer

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 :

  global $_wp_additional_image_sizes;
  if ( array_key_exists( 'desired-image-size' , $_wp_additional_image_sizes) )
  {
    //your code here
  }
  else
  {
    //default code here 
  }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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