...

移除WordPress文章图片的宽度和高度属性

内容导航

通过WordPress自身的媒体上传功能插入到文章的图片,都会默认添加了高度和宽度属性:

<img title="使用 Chrome Workspace 进行网站调试 | 倡萌的自留地 cmhello.com" alt="使用 Chrome Workspace 进行网站调试" src="http://wordpess.com/cmhello/Chrome-Workspace_9515/0019-cmhello_com.png" width="550" height="307" />

如果出于某些原因,你想去掉这个高度和宽度属性,可以将下面的代码添加到主题的 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;
}
<script>
$(document).ready(function() {
$(".main_box img").removeAttr("width height");
});
</script>

躺拿询盘的外贸独立站,
打开邮箱,就有优质客户!

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.