欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WordPress去除img標簽的高度與寬度讓圖片自適應屏幕

  發(fā)布時間:2014-03-20 15:50:53   作者:佚名   我要評論
在移動設備端,因為屏幕都比較小,如果要讓圖片自適應屏幕,我們應當把width和height屬性去除,不然圖片可能會比屏幕大,有此需求的朋友可以參考下本文
要求

如,在桌面設備上,圖片使用的是以下的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()) {
// 刪除文章內(nèi)容中img的width和height屬性
add_filter('the_content', 'ludou_remove_width_height_attribute', 99);
}

這樣我再試一下是不是達到想要的結果了。

相關文章

最新評論