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标签
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
wordpress主题中无奈显示阅读量成绩的处理办法

而后你犯晕了吧。次要缘由是某些主题没有增加WP-postviews这款插件的支持代码,凑巧笔者的Green Park 2这款...

WordPress中的shortcode短代码性能应用详解

WordPress 从 2.5 的版本开端,添加了一个 shortcode (短代码) API ,相似于 BBS 上的 BBCode , shortcode ...

WordPress首页显示多个图片及文字友谊链接的办法

本文实例讲述了WordPress首页显示多个图片及文字友谊链接的办法。分享给大家供大家参考。详细剖析如下: Wo...

wordpress中弱小的调用文章函数query posts 用法

query posts是一个十分好用的调用文章函数,能够做到同页面内显示多种特定范畴的文章,例如能够调用某分类、...

WordPress主题制造之模板文件的引入办法

get_template_part() 用来援用模板文件,相似于 get_header()、get_sidebar() 和 get_footer(),只不过这个...

wordpress暴门路 影响一切版本

缺点文件/{Path}/wp-includes/registration-functions.php 要害代码: &mdash;-> <?php /** * Deprecated. N...

需求提交

客服服务

亿鸽在线客服系统