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

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

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

如果出于某些原因,你想去掉这个高度和宽度属性,可以将下面的代码添加到主题的 functions.php 文件即可:

1 add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
2 add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
3  
4 function remove_width_attribute( $html ) {
5    $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
6    return $html;
7 }

 有些不生效的

在footer.php中添加

1 <script>
2 $(document).ready(function() {
3     $(".main_box img").removeAttr("width height");
4 });
5 </script>
main_box改为文中原属性

转载于:https://www.cnblogs.com/php-qiuwei/p/9843365.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值