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主动给文章增加nofollow属性的完成办法

本文实例讲述了WordPress主动给文章增加nofollow属性的完成办法。分享给大家供大家参考。详细剖析如下: no...

wordpress增加顶级菜单完成思绪

比方本人创立了一个主题,那么需求在后盾增加一些设置选项,所以就触及到了后盾增加顶级菜单的需要: 复制代...

且谈WordPress功能优化分享

作为一款遭到大少数站长欢送的内容治理零碎,WordPress不只有着弱小的治理性能,而且应用不便,即便是老手也...

wordpress评论者链接在新窗口中关上的办法

找到wp-includes/comment-template.php文件中 复制代码代码如下:if ( empty( $url ) || 'http://' == $url ...

详解WordPress开发中用于获取分类及子页面的函数用法

get_category get_category 可能咱们平常接触的不多,但却是很有用,网上这个函数引见的貌似不多,所以明天...

WordPress完成网站投稿者也能够上传图片的办法

本文实例讲述了WordPress完成网站投稿者也能够上传图片的办法。分享给大家供大家参考。详细剖析如下: Word...

需求提交

客服服务

亿鸽在线客服系统