Only Display Thumbnail if Larger Than

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

Related posts

2 comments

  1. 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

  2. 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.

Comments are closed.