我试图在我的wordpress主题中实现响应性图像。有几个插件可用,但那些我试图搞乱我的布局。
基本上,我想要做的是用响应性的图像替换所有现有的图像(不仅仅是特征图像或缩略图),或者用srcset或者
我的wordpress主题创建了以下图像大小:
add_image_size( "maximal", "1900" );
add_image_size( "desktop", "1405" );
add_image_size( "tablet", "981" );
add_image_size( "smalltablet", "768" );
add_image_size( "mobile", "479" );我尝试用preg_replace函数替换img标记,但我从未成功捕获所有图像,我也不确定这是否是最好的方法。我的一些图像有类,有些则没有。
最后,我想用这样的简单图片来代替:
<img src="<destination"> id="<id>" class="<class">就像这样:
<picture>
<source srcset="<img src for mobile" media="(max-width: 500px)">
<source srcset="<img src for small tablets" media="(min-width: 700px)">
<source srcset="<img src for tablets" media="(min-width: 950px)">
<source srcset="<img src for desktop" media="(min-width: 1200px)">
<source srcset="<img src for big screens" media="(min-width: 1600px)">
</picture>srcset等价物也可以。
谢谢!洛朗
发布于 2015-11-11 10:38:02
我认为过滤器post_thumbnail_html会起作用的,代码应该是这样的..
add_filter('post_thumbnail_html', 'picturefill_html', 99, 5);
function picturefill_html(){
$id = get_post_thumbnail_id(); // get attachment id
$src_small = wp_get_attachment_image_src($id, 'smalltablet'); // your new image size url
$src_large = wp_get_attachment_image_src($id, 'maximal'); // your another new image size url
$class = $attr['class'];
$html = '
<picture>
<source srcset="'.$src_small[0].'" media="(max-width: 500px)">
<source srcset="'.$src_large[0].'" media="(min-width: 1200px)">
</picture>
';
return $html;
}发布于 2016-01-04 14:13:25
Picturefill现在集成在Wordpress中,您可以使用以下方法检索图像源和源集:
wp_get_attachment_image_url( get_post_thumbnail_id( [PUT POST ID HERE]), 'medium' );获取图像源集:
wp_get_attachment_image_srcset( get_post_thumbnail_id([PUT POST ID HERE] ), 'medium' );这样容易多了!
谢谢劳伦
https://stackoverflow.com/questions/31407260
复制相似问题