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

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

1
<img title="使用 Chrome Workspace 进行网站调试 | 的自留地 cmhello.com" alt="使用 Chrome Workspace 进行网站调试" src="/wp-content/imgs/1118/0019-cmhello_com.png" width="550" height="307" />

<img title="使用 Chrome Workspace 进行网站调试 | 的自留地 cmhello.com" alt="使用 Chrome Workspace 进行网站调试" src="/wp-content/imgs/1118/0019-cmhello_com.png" width="550" height="307" />

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

1
2
3
4
5
6
7
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;
}

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;
}

参考资料:http://css-tricks.com/snippets/wordpress/remove-width-and-height-attributes-from-inserted-images/

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号