我正在尝试在我的 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 等效项也可以。
谢谢!洛朗