Is there a way to only display <?php the_post_thumbnail( $size, $attr ); ?> if it is larger/smaller than specified dimensions?

2 Answers 2


You may be able to make this work with a filter on post_thumbnail_html.

function filter_thumb_html($html, $post_id, $post_thumbnail_id, $size, $attr ) {
  $dimensions = wp_get_attachment_image_src($post_thumbnail_id, $size);
  if ($dimensions[1] > 500 || $dimensions[2] > 500) {
    return '';

I am not sure what you mean by "if it is larger/smaller...". I don't know if need both conditions at once, or one or the other, or if you need to change the restrictions dynamically. The code above should give you a working model though.




This should work

$width = 200;
$height = 200;

$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );

if($image[1] >= $width && $image[2] >= $height ){
    the_post_thumbnail($size, $attr);

Define width and height, and the size and attr that you want.

  • This did not work for me. Jun 4, 2013 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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