wp_get_attachment_image_srcset

函式
wp_get_attachment_image_srcset ( $attachment_id, $size = 'medium', $image_meta = null )
引數
  • (int) $attachment_id Image attachment ID.
    Required:
  • (string|int[]) $size Optional. Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'medium'.
    Required:
    Default: 'medium'
  • (array) $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. Default null.
    Required:
    Default: null
返回值
  • (string|false) A 'srcset' value string or false.
相關
  • wp_calculate_image_srcset()
定義位置
相關方法
wp_get_attachment_image_srcwp_get_attachment_image_urlwp_get_attachment_image_sizeswp_get_attachment_imagewp_get_attachment_url
引入
4.4.0
棄用
-

wp_get_attachment_image_srcset: 這個函式返回一個陣列,包含特定圖片附件的圖片來源和尺寸。這對於生成響應式圖片很有用。

檢索一個影象附件的’srcset’屬性的值。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
$image = wp_get_attachment_image_src( $attachment_id, $size );
if ( ! $image ) {
return false;
}
if ( ! is_array( $image_meta ) ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
}
$image_src = $image[0];
$size_array = array(
absint( $image[1] ),
absint( $image[2] ),
);
return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
}
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) { $image = wp_get_attachment_image_src( $attachment_id, $size ); if ( ! $image ) { return false; } if ( ! is_array( $image_meta ) ) { $image_meta = wp_get_attachment_metadata( $attachment_id ); } $image_src = $image[0]; $size_array = array( absint( $image[1] ), absint( $image[2] ), ); return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); }
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
	$image = wp_get_attachment_image_src( $attachment_id, $size );

	if ( ! $image ) {
		return false;
	}

	if ( ! is_array( $image_meta ) ) {
		$image_meta = wp_get_attachment_metadata( $attachment_id );
	}

	$image_src  = $image[0];
	$size_array = array(
		absint( $image[1] ),
		absint( $image[2] ),
	);

	return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
}

常見問題

FAQs
檢視更多 >