删除或失效WordPress文章中的图像大小属性

默认情况下,WordPress会将图像元素widthheight属性添加到图像元素中。这些属性会影响CSS宽度和高度属性,图片延迟加载时默认图片的大小,可通过 PHP、JavaScript 和 CSS 来删除属性,或者使用其失效。

从媒体库插入的图像中删除图像大小属性

删除图像大小属性可完全控制 CSS 属性,可将以下代码添加到主题 functions.php 文件中:

/**
* 移除图片高度和宽度属性从文章内容中的图片上
*/
function salong_remove_image_size_attributes( $html ) {
return preg_replace( '/(width|height)="\d*"/', '', $html );
}
// 从特色图像中删除图片大小属性
add_filter( 'post_thumbnail_html', 'salong_remove_image_size_attributes' );
// 从添加到WordPress文章的图像中删除图像大小属性
add_filter( 'image_send_to_editor', 'salong_remove_image_size_attributes' );

请注意,当图像是特色图像或将媒体库有图像添加到文章时,此代码将从图像中去除图像大小属性,再添加图像到文章中。已上传到文章中的现有图像不受影响。

通过 jQuery 删除widthheight属性

对于已经添加到文章的图像,必须手动删除widthheight属性,或者也可以使用一些jQuery代码来解决问题,以下代码添加到主题 js 文件中:

/*直接删除图像上的大小属性*/
jQuery(document).ready(function($) {
    $('img').removeAttr('width').removeAttr('height');
});

使用 jQuery 代码删除图像大小属性更加方便,对于已经添加或者将来要添加的图片都适用。

使用 CSS 使图像大小属性失效

对于响应式图片或者延迟加载时的默认图片都是较好的解决方法,将以下代码添加到主题 CSS 样式文件中:

img {
    width: initial !important;
    max-width: 100% !important;
}

对于延迟加载时,默认图像与实际图像大小不一样时,默认图像将保持原有的大小。

英文原文:https://wpscholar.com/blog/remove-wp-image-size-attributes/

本文原创,作者:萨龙龙,其版权均为4166am金沙信心之选所有。
如需转载,请注明出处:/remove-wordpress-image-size-attributes.html



\u4ece\u5a92\u4f53\u5e93\u2026@\u8428\u9f99\u7f51\u7edc&appkey=746468751&pic=https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg&searchPic=true","qq":"https:\/\/connect.qq.com\/widget\/shareqq\/index.html?url=https:\/\/salongweb.com\/remove-wordpress-image-size-attributes.html&title=\u5220\u9664\u6216\u5931\u6548WordPress\u6587\u7ae0\u4e2d\u7684\u56fe\u50cf\u5927\u5c0f\u5c5e\u6027&pics=https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg&summary=\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cWordPress\u4f1a\u5c06\u56fe\u50cf\u5143\u7d20width\u548cheight\u5c5e\u6027\u6dfb\u52a0\u5230\u56fe\u50cf\u5143\u7d20\u4e2d\u3002\u8fd9\u4e9b\u5c5e\u6027\u4f1a\u5f71\u54cdCSS\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u5c5e\u6027\uff0c\u56fe\u7247\u5ef6\u8fdf\u52a0\u8f7d\u65f6\u9ed8\u8ba4\u56fe\u7247\u7684\u5927\u5c0f\uff0c\u53ef\u901a\u8fc7PHP\u3001JavaScript\u548cCSS\u6765\u5220\u9664\u5c5e\u6027\uff0c\u6216\u8005\u4f7f\u7528\u5176\u5931\u6548\u3002



\u4ece\u5a92\u4f53\u5e93\u2026&site=\u8428\u9f99\u7f51\u7edc","zone":"https:\/\/sns.qzone.qq.com\/cgi-bin\/qzshare\/cgi_qzshare_onekey?url=https:\/\/salongweb.com\/remove-wordpress-image-size-attributes.html&title=\u5220\u9664\u6216\u5931\u6548WordPress\u6587\u7ae0\u4e2d\u7684\u56fe\u50cf\u5927\u5c0f\u5c5e\u6027&desc=\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cWordPress\u4f1a\u5c06\u56fe\u50cf\u5143\u7d20width\u548cheight\u5c5e\u6027\u6dfb\u52a0\u5230\u56fe\u50cf\u5143\u7d20\u4e2d\u3002\u8fd9\u4e9b\u5c5e\u6027\u4f1a\u5f71\u54cdCSS\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u5c5e\u6027\uff0c\u56fe\u7247\u5ef6\u8fdf\u52a0\u8f7d\u65f6\u9ed8\u8ba4\u56fe\u7247\u7684\u5927\u5c0f\uff0c\u53ef\u901a\u8fc7PHP\u3001JavaScript\u548cCSS\u6765\u5220\u9664\u5c5e\u6027\uff0c\u6216\u8005\u4f7f\u7528\u5176\u5931\u6548\u3002



\u4ece\u5a92\u4f53\u5e93\u2026&summary=&site=\u8428\u9f99\u7f51\u7edc"}>