WordPress去除img标签的高度与宽度让图片自顺应屏幕

要求

如,在桌面设施上,图片应用的是以下的HTML代码:
 
<img src="abc.png" alt="abc" width="580" height="267" />

在挪动设施端,由于屏幕都比拟小,假如要让图片自顺应屏幕,咱们该当把width和height属性去除,不然图片可能会比屏幕大:
 
<img src="abc.png" alt="abc" />

办法一,
将上面代码复制到以后主题的 functions.php 文件中:
 
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}

办法二
 
// 自顺应图片删除width和height,by Ludou
function ludou_remove_width_height_attribute($content){
preg_match_all("/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png\.bmp]))[\'|\"].*?[\/]?>/", $content, $images);
if(!empty($images)) {
foreach($images[0] as $index => $value){
$new_img = preg_replace('/(width|height)="\d*"\s/', "", $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
return $content;
}
// 判别能否是挪动设施阅读
if(wp_is_mobile()) {
// 删除文章内容中img的width和height属性
add_filter('the_content', 'ludou_remove_width_height_attribute', 99);
}

这样我再试一下是不是达到想要的后果了。

以上就是安达网络工作室关于《WordPress去除img标签的高度与宽度让图片自适应屏幕》的一些看法。更多内容请查看本栏目更多内容!

本文相关话题: wordPress 高度与宽度 img标签
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
开发者应该注意的WordPress2.9新性能小结

1.文章缩略图(Post Thumbnails) WordPress 2.9 一个比拟重头的新性能就是提供了不需求自定义字段的文章缩略...

以WordPress为例解说jQuery丑化页面Title的办法

这里选取的例子,便是 WordPress 中比拟有名的丑化超链接Title成果,普通的 title 成果是把鼠标放到 a 元素...

WordPress中调试缩略图的相干PHP函数应用解析

the_post_thumbnail the_post_thumbnail 在 WordPress 中次要用来打印文章中设定的缩略图,而 get_the_post...

WordPress 部署 Minify办法 主动紧缩兼并 JS 和 CSS

随着 WordPress 一版一版的晋级,性能越来越多,代码量也越来越宏大,对 WordPress 速度慢的埋怨声也越来越...

wordpress自定义上传文件类型的办法

前言 家喻户晓WordPress默许支持大局部图片等文件格局的上传,但也有一些文件格局是不支持的,依据集体需求...

WordPress JQuery解决沙发头像

我想这对沙发同窗不太偏心吧。昨天在看yinheli同窗的《鼠标悬浮完成显示留言内容》一文(倡议看看,好文章)...

需求提交

客服服务