我想为每个帖子、页面和主页移除 Wordpress 中第一张图片 上的延迟加载。
我没有使用任何插件来延迟加载图像,我需要一个解决方案来解决默认延迟加载的问题。
我找到了 this script对于 functions.php,但它是用于 WP Rocket 插件的,我没有这个插件。
所以我修改了它:
function add_responsive_class($content){
if ( is_single() || is_page() || is_front_page() || is_home() ) {
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$document = new DOMDocument();
libxml_use_internal_errors(true);
$document->loadHTML(utf8_decode($content));
$imgs = $document->getElementsByTagName('img');
$img = $imgs[0];
if ($imgs[0] == 1) { // Check if the post has images first
$img->setAttribute('class','aligncenter size-full remove-lazy');
$html = $document->saveHTML();
return $html;
}
else {
return $content;
}
}
else {
return $content;
}
}
add_filter ('the_content', 'add_responsive_class');
现在,我需要只移除类为“aligncenter size-full remove-lazy”的图像上的延迟加载。
我在上面的代码之后尝试了这个,但是我有一个错误并且它不起作用:
function remove_lazy_loading_for_specific_class( $attributes ) {
$attributes[] = 'class="aligncenter size-full remove-lazy"';
return $attributes;
}
add_filter( 'wp_img_tag_add_loading_attr', 'remove_lazy_loading_for_specific_class' );
我该怎么做?谢谢
最佳答案
不确定这是否有帮助,但不是所有这些,为什么不给这个图像一个 ID 或一个类,并删除 WordPress 默认添加的 lazy
属性,使用 removeAttribute( )
方法。
关于php - 删除 Wordpress 中第一张图片的延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65323823/