删除 Wordpress 中第一张图片的延迟加载
Remove Lazy loading on first image in Wordpress
我想删除 Wordpress 中每个帖子、页面和主页的第一张图片 上的延迟加载。
我没有使用任何延迟加载图片的插件,我需要一个由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');
现在,我需要使用 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 或 class 并删除 WordPress 默认使用 [= 添加的 lazy
属性而不是所有这些11=]方法。
我只用这个解决了:)
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->removeAttribute( 'loading' );
$html = $document->saveHTML();
return $html;
}
else {
return $content;
}
}
else {
return $content;
}
}
add_filter ('the_content', 'add_responsive_class');
我想删除 Wordpress 中每个帖子、页面和主页的第一张图片 上的延迟加载。
我没有使用任何延迟加载图片的插件,我需要一个由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');
现在,我需要使用 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 或 class 并删除 WordPress 默认使用 [= 添加的 lazy
属性而不是所有这些11=]方法。
我只用这个解决了:)
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->removeAttribute( 'loading' );
$html = $document->saveHTML();
return $html;
}
else {
return $content;
}
}
else {
return $content;
}
}
add_filter ('the_content', 'add_responsive_class');