1 回答

TA贡献1830条经验 获得超9个赞
从 5.6.0 开始,我们有了一种包装 img 元素的方法。
apply_filters( 'wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr );
过滤器不提取 post_id 而是提取 attachment_id。因此,如果您想系统地激活该功能,您应该通过帖子元链接附件及其移动图像。
function wrap_wp_get_attachment_image_with_picture_tag( $html, $attachment_id, $size, $icon, $attr ){
if ( is_admin() || $size == 'medium' || $size == 'thumbnail' ) return $html;
if ( $mobile_id = get_post_meta( $attachment_id, '_meta_key_of_mobile_picuture_id', true ) ){
$mobile_srcset = wp_get_attachment_image_srcset( $mobile_id, 'medium_large' );
$html = '<picture><source media="( max-width : 782px )" srcset="'.$mobile_srcset.'">'.$html.'</picture>';
}
return $html;
}
add_filter( 'wp_get_attachment_image', 'wrap_wp_get_attachment_image_with_picture_tag', 10, 5 );
- 1 回答
- 0 关注
- 124 浏览
添加回答
举报