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 php 分页代码

成果: 将上面的函数放到你的主题的 functions.php 文件中: 代码如下: function theme_echo_pagenavi(){ gl...

保障WordPress平安的完成办法

小洞不补大洞享乐。关于bloggers来说这是永恒的真谛,仅仅花一点工夫在马上就晋级上省下了很多之后修复一些...

WordPress站点完成分类目录订阅性能实例

WordPress有着比拟弱小的分类机制,包括文章分类、标签等,弱小的分类机制能够用来完成门户网站的相似二级频...

WordPress中给文章增加自定义字段及后盾编辑性能区域

add_post_meta add_post_meta 函数是 WordPress 中用来给文章或页面增加自定义字段值的一个函数, 其用法与...

wordpress自定义摘要截取字数的代码

复制代码代码如下: function excerpt($limit) { $excerpt = explode(' ', get_the_excerpt(), $limit); i...

在WordPress中应用wp-cron插件来设置定时义务

PHP 自身是无奈创立定时义务的,然而 WordPress 自带了一个伪定时义务(Cron) API,十分的不便好用,包括 ...

需求提交

客服服务

亿鸽在线客服系统