1

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

0

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 '';
  }
}
add_filter('post_thumbnail_html','filter_thumb_html',1,5);

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.

Reference

http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/post-thumbnail-template.php#L85

0

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.

1
  • This did not work for me. – AndrettiMilas Jun 4 '13 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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