
在移动设备端,由于屏幕较小,如果要使图片响应屏幕,就要去掉宽度和高度属性,否则图片很可能比屏幕大。有这个要求的可以参考以下。
规定如,在桌面上机器设备上,图片应用的是下列的HTML代码:
复制代码代码以下:
<imgsrc="abc.png"alt="abc"width="580"height="267"/>
在移动设备端,由于屏幕都较为小,假如要让图片响应式屏幕,大家理应把width和height属性除去,要不然图片很有可能会比屏幕大:
复制代码代码以下:
<imgsrc="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);
functionremove_width_attribute($html){
$html=preg_replace('/(width|height)="\d*"\s/',"",$html);
return$html;
}
方式二
复制代码代码以下:
//响应式图片删掉width和height,byLudou
functionludou_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);
}
那样我再试一下是否做到要想的結果了。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)