Created
December 12, 2017 11:08
-
-
Save jimmy89Li/ea70c56241f519df68fb07068b885d16 to your computer and use it in GitHub Desktop.
Display medium size images without srcset in WordPress
This file contains 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 | |
// Add support for cropping default WordPress medium images | |
if(!get_option("medium_crop")) { | |
add_option("medium_crop", "1"); | |
} else { | |
update_option("medium_crop", "1"); | |
} | |
update_option( 'medium_size_w', 200 ); | |
update_option( 'medium_size_h', 300 ); | |
/** | |
* Disable responsive image support | |
*/ | |
// Clean the up the image from wp_get_attachment_image() | |
add_filter( 'wp_get_attachment_image_attributes', function( $attr ) { | |
if( isset( $attr['sizes'] ) ) unset( $attr['sizes'] ); | |
if( isset( $attr['srcset'] ) ) unset( $attr['srcset'] ); | |
return $attr; | |
}, PHP_INT_MAX ); | |
// Override the calculated image sizes | |
add_filter( 'wp_calculate_image_sizes', '__return_empty_array', PHP_INT_MAX ); | |
// Override the calculated image sources | |
add_filter( 'wp_calculate_image_srcset', '__return_empty_array', PHP_INT_MAX ); | |
// Remove the reponsive stuff from the content | |
remove_filter( 'the_content', 'wp_make_content_images_responsive' ); | |
?> |
This file contains 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 echo wp_get_attachment_image( $image['ID'],'medium' ); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment