WordPress srcset only from the required sizes. Code examples open

WordPress srcset only from the required sizes. Code examples

function sls_get_image_srcset( $slide_id ) {

						$wp_image_attributes = wp_get_attachment_image_src( $slide_id, 'full' );

                        

						$check_full_size = $wp_image_attributes[1];

						$sizes = get_intermediate_image_sizes();

						foreach ( $sizes as $size ) {

							$info = wp_get_registered_image_subsizes()[ $size ];

							// echo $size . ' (' . $info['width'] . 'x' . $info['height'] . ')<br>';
			
							if ( $size == 'medium' && $check_full_size >= $info['width'] ) {
								$image_size_medium     = wp_get_attachment_image_src( $slide_id, 'medium' );
								$img_size_medium_width = $info['width'];
							}

							if ( $size == 'medium_large' && $check_full_size >= $info['width'] ) {
								$image_size_medium_large     = wp_get_attachment_image_src( $slide_id, 'medium_large' );
								$img_size_medium_large_width = $info['width'];
							}

							if ( $size == 'large' && $check_full_size >= $info['width'] ) {
								$image_url_large  = wp_get_attachment_image_src( $slide_id, 'large' );
								$image_size_large = $info['width'];
							}

						}

						if ( isset( $image_size_large ) ) {
							if ( $check_full_size > $image_size_large ) {
								$image_url_full = $wp_image_attributes[0];
								$img_size_full  = $wp_image_attributes[1];
							}
						}
					}
					?>
					
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation