Skip to content

Instantly share code, notes, and snippets.

@sagarjadhav
Last active April 21, 2017 12:15
Show Gist options
  • Save sagarjadhav/fb2b573433d027e2d50d4f5a764e03e2 to your computer and use it in GitHub Desktop.
Save sagarjadhav/fb2b573433d027e2d50d4f5a764e03e2 to your computer and use it in GitHub Desktop.
Image cropping fix
<?php
if ( has_post_thumbnail( $post_id ) ) {
// Get Featured Image
$class = 'img-thumb';
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$image_exists = wp_get_attachment_image_src( $post_thumbnail_id, 'thumbnail-size' );
if ( !$image_exists[ 3 ] ) {
$class = 'img-not-thumb';
}
?>
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'marvy' ), the_title_attribute( 'echo=0' ) ) ); ?>" class="<?php echo $class; ?>">
<?php the_post_thumbnail( 'thumbnail-size' ); ?>
</a><?php
}
// Following css required
echo '
.img-not-thumb {
position: relative;
overflow: hidden;
padding-bottom: 69.44%; /* Calculated = height / width * 100 */
display: block;
}
.img-not-thumb img {
position: absolute;
top: 0;
/*bottom: 0;*/
left: 0;
right: 0;
margin: auto;
z-index: 0;
min-height: 100%;
width: auto;
/*max-height: 100%;*/
max-width: 100%;
min-width: 100%;
object-fit: cover;
}
';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment