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 dynamic_sidebar()函数应用办法

dynamic_sidebar()函数用来支持自定义sidebar侧边栏,能够自定义Widget插件,比方为侧边栏增加最近文章,文...

WordPress完成的首页幻灯片展现性能示例【附demo源码】

本文实例讲述了WordPress完成的首页幻灯片展现性能。分享给大家供大家参考,详细如下: 关于WordPress拓展性...

WordPress导航菜单函数wp_nav_menu()详解

1、wp_nav_menu()函数引见:worpdress倒退到3.0当前添加了一个自定义菜单函数wp_nav_menu(),使得wordpress...

wordpress全局变量$wpdb初始化并申明为全局变量的办法

首先从wordpress顺序index.php动手: 复制代码代码如下:define('WP_USE_THEMES', true); /** Loads the Wor...

wordpress装置出错的处理办法

明天在装置wordpress3.0.1的时分,写入数据库阶段时呈现了一大片谬误提醒信息: 复制代码代码如下:WordPres...

在SAE上搭建最新wordpress的办法

装置SAE上的wordpress,创立使用抉择wordpress模板,装置后是3.4版本 新建一个版本2,下载最新wordpress装置...

需求提交

客服服务

亿鸽在线客服系统