From 942c6495657651b586e02e754a684c0dbb045d8f Mon Sep 17 00:00:00 2001 From: Morten Rand-Hendriksen Date: Sun, 2 Dec 2018 15:00:00 -0800 Subject: [PATCH 1/2] Fix `sizes` attribute for regular aligned body images. Does not solve for `alignwide` and `alignfull` images, but is an improvement over core. --- inc/template-functions.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/inc/template-functions.php b/inc/template-functions.php index 9b860aa9..edc93354 100644 --- a/inc/template-functions.php +++ b/inc/template-functions.php @@ -131,6 +131,27 @@ function twentynineteen_post_thumbnail_sizes_attr( $attr ) { } add_filter( 'wp_get_attachment_image_attributes', 'twentynineteen_post_thumbnail_sizes_attr', 10, 1 ); +/** + * Add custom `sizes` attribute to responsive image functionality for post content images. + * + * @since Twenty Nineteen 1.0 + * + * @param string $sizes A source size value for use in a 'sizes' attribute. + * @param array $size Image size. Accepts an array of width and height + * values in pixels (in that order). + * @return string A source size value for use in a content image 'sizes' attribute. + */ +function twentynineteen_calculate_image_sizes_attr( $sizes ) { + if ( is_admin() ) { + return $sizes; + } + + $sizes = '(min-width: 768px) calc(8 * (100vw / 12) - 28px), (min-width: 1168) calc(6 * 100vw/12) - 28px), calc(100% - (2 * 1rem))'; + + return $sizes; +} +add_filter( 'wp_calculate_image_sizes', 'twentynineteen_calculate_image_sizes_attr', 10, 1 ); + /** * Returns the size for avatars used in the theme. */ From c481af65c7fb26732d929513edc9f7731f8f0659 Mon Sep 17 00:00:00 2001 From: Morten Rand-Hendriksen Date: Sun, 2 Dec 2018 15:05:19 -0800 Subject: [PATCH 2/2] Make sure `sizes` are not rewritten for smaller images. --- inc/template-functions.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/inc/template-functions.php b/inc/template-functions.php index edc93354..160d4d8c 100644 --- a/inc/template-functions.php +++ b/inc/template-functions.php @@ -141,16 +141,20 @@ function twentynineteen_post_thumbnail_sizes_attr( $attr ) { * values in pixels (in that order). * @return string A source size value for use in a content image 'sizes' attribute. */ -function twentynineteen_calculate_image_sizes_attr( $sizes ) { +function twentynineteen_calculate_image_sizes_attr( $sizes, $size ) { if ( is_admin() ) { return $sizes; } - $sizes = '(min-width: 768px) calc(8 * (100vw / 12) - 28px), (min-width: 1168) calc(6 * 100vw/12) - 28px), calc(100% - (2 * 1rem))'; + $width = $size[0]; + + if ( 767 <= $width ) { + $sizes = '(min-width: 768px) calc(8 * (100vw / 12) - 28px), (min-width: 1168) calc(6 * 100vw/12) - 28px), calc(100% - (2 * 1rem))'; + } return $sizes; } -add_filter( 'wp_calculate_image_sizes', 'twentynineteen_calculate_image_sizes_attr', 10, 1 ); +add_filter( 'wp_calculate_image_sizes', 'twentynineteen_calculate_image_sizes_attr', 10, 2 ); /** * Returns the size for avatars used in the theme.