Customize image “srcset” and “sizes”
-
Hello everyone, everything good?
I’m looking for a way to customize the “srcset” and “sizes” attributes of all the images in my posts (the ones that have already been posted, and the ones that will be posted), however, I’m only managing to modify the “srcset” attribute by adding the following code to the functions.php file:
function custom_image_srcset_and_sizes($sources, $size_array, $image_src, $image_meta, $attachment_id) { $aux = explode('/', $image_meta['file']); //fictitious link $link = 'https://www.exemple.com/blog/wp-content/uploads/' . $aux[0] . '/' . $aux[1] . '/'; //opening the new "srcset" $sources['720'] = array( 'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'], 'descriptor' => 'w', 'value' => '720', ); $sources['543'] = array( 'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'], 'descriptor' => 'w', 'value' => '543', ); $sources['360'] = array( 'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'], 'descriptor' => 'w', 'value' => '360', ); $sources['280'] = array( 'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'], 'descriptor' => 'w', 'value' => '280', ); return $sources; } add_filter('wp_calculate_image_srcset', 'custom_image_srcset_and_sizes', 10, 5);
Could someone tell me how do I modify the “sizes” attribute (there are not the “height” and “width” attributes, is it really the “sizes” attribute)?
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Customize image “srcset” and “sizes”’ is closed to new replies.