Last active
April 21, 2017 12:15
-
-
Save sagarjadhav/fb2b573433d027e2d50d4f5a764e03e2 to your computer and use it in GitHub Desktop.
Image cropping fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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