今天遇到一个同事做的企业网站,在移动端访问的时候产品图片被限定固定高度和宽度。因为默认WordPress程序编辑文章上传图片后,会自动添加图片宽带和高度尺寸。但是如果到移动端的话,那 就限定图片高度和宽度后会撑大页面。
这里网上找到解决办法可以在移动端访问后去掉宽度和高度限制:


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

将代码添加到当前主题Functions.php文件中。就可以在移动端取消宽度和高度限制。


扫描二维码,在手机上阅读!