当我使用get_the_post_thumbnail( NULL, \'entry-fullwidth\', NULL );
wordpress自动输出srcset
以下数据:
<img
width="1000"
height="625"
src="https://x.cloudfront.net/wp-content/uploads/2017/08/photo.jpg"
class="attachment-entry-fullwidth size-entry-fullwidth wp-post-image"
alt=""
srcset="
https://x.cloudfront.net/wp-content/uploads/2017/08/photo.jpg 1000w,
https://x.cloudfront.net/wp-content/uploads/2017/08/photo-768x480.jpg 768w"
sizes="
(max-width: 767px) 95vw,
(max-width: 979px) 80vw, 1200px
">
在下面的自定义函数中,是否有本机wordpress函数或其他方式输出图像的srcset数据?
function photo_shortcode($atts){
extract(shortcode_atts(array(
\'no\' => 1,
), $atts));
$no = ( $no != \'\' ) ? $no : 1;
$images = get_field(\'fl_gallery\');
$image = $images[$no];
if ($image) {
$credit = get_field(\'fl_credit\', $image[\'id\']);
return \'<div class="kalim"><img title="\' . esc_attr( sprintf( the_title_attribute( \'echo=0\' ) ) ) . \'" alt="\' . esc_attr( sprintf( the_title_attribute( \'echo=0\' ) ) ) . \'" src="\' . $image[\'url\'] . \'" /><div class="kalca">\' . $image[\'caption\'] . \'</div>\' . (!empty($credit) ? \'<div class="kalcr">Credit:\' . $credit . \'</div></div>\': \'\' ) ;
}
}
add_shortcode(\'photo\', \'photo_shortcode\');
最合适的回答,由SO网友:Johansson 整理而成
我想wp_get_attachment_srcset()
就是你要找的。
$srcset = wp_get_attachment_image_srcset( $image[\'id\'], array( 100, 100 ) );
现在,您可以转义HTML并在代码中使用它。
<img src="PATH HERE" srcset="<?php echo esc_attr( $srcset ); ?>">