• При отображении картинки в атрибуте srcset есть только три варианта. Как добавить свой собственный?

Просмотр 2 ответов — с 1 по 2 (всего 2)
  • Нашел

    /**
    * filter function to force wordpress to add our custom srcset values
    * @param array  $sources {
    *     One or more arrays of source data to include in the 'srcset'.
    *
    *     @type type array $width {
    *          @type type string $url        The URL of an image source.
    *          @type type string $descriptor The descriptor type used in the image candidate string,
    *                                        either 'w' or 'x'.
    *          @type type int    $value      The source width, if paired with a 'w' descriptor or a
    *                                        pixel density value if paired with an 'x' descriptor.
    *     }
    * }
    * @param array  $size_array    Array of width and height values in pixels (in that order).
    * @param string $image_src     The 'src' of the image.
    * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    * @param int    $attachment_id Image attachment ID.
    
    * @author: Aakash Dodiya
    * @website: http://www.developersq.com
    */
    add_filter( 'wp_calculate_image_srcset', 'dq_add_custom_image_srcset', 10, 5 );
    function dq_add_custom_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ){
    
    	$image_basename = wp_basename( $image_meta['file'] );
    	$image_baseurl = _wp_upload_dir_baseurl();
    	// Uploads are (or have been) in year/month sub-directories.
    	if ( $image_basename !== $image_meta['file'] ) {
    		$dirname = dirname( $image_meta['file'] );
    
    		if ( $dirname !== '.' ) {
    			$image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    		}
    	}
            // get image baseurl
    	$image_baseurl = trailingslashit( $image_baseurl );
    	// check whether our custom image size exists in image meta
    	if( array_key_exists('wt375_253', $image_meta['sizes'] ) ){
    		// add source value to create srcset
    		$sources[ $image_meta['sizes']['wt375_253']['width'] ] = array(
    				 'url'        => $image_baseurl .  $image_meta['sizes']['wt375_253']['file'],
    				 'descriptor' => 'w',
    				 'value'      => $image_meta['sizes']['wt375_253']['width'],
    		);
    	}
    
            //return sources with new srcset value
    	return $sources;
    }

    scrset — если размер изображения на странице 300px или меньше, то будет использоваться image-300×200.jpg, если от 300px до 700px то image-700×620.jpg. В случае, если ни одно из условий не удовлетворится, то image.jpg.

    Такой вопрос. У меня ширина экрана 320px.
    Почему грузится полное изображение?

    <img class=»wp-image-203 size-full» style=»max-width: 100%;» title=»Мыс Бурхан» src=»http://www.ss.ru/wp-content/uploads/2015/09/burh1.jpg&#187; alt=»Мыс Бурхан» height=»239″ srcset=»http://ss.ru/wp-content/uploads/2015/09/burh1-300×100.jpg 300w, http://ss.ru/wp-content/uploads/2015/09/burh1.jpg 720w, http://ss.ru/wp-content/uploads/2015/09/burh1-375×239.jpg 375w» sizes=»(max-width: 720px) 100vw, 720px»>

Просмотр 2 ответов — с 1 по 2 (всего 2)

Тема «Добавить свои варианты в srcset» закрыта для новых ответов.