Hi @yasir129
Thank you very much by your return and solution!
I have difficulties, but i′m learning a little every day.
After visiting the first link, i have applied this code for my home page: (4 images above the fold)
function add_responsive_class($content){
if (is_page('ID OF HOME')) {
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$document = new DOMDocument();
libxml_use_internal_errors(true);
$document->loadHTML(utf8_decode($content));
$imgA = $document->getElementsByTagName('img')->item(0);
$imgB = $document->getElementsByTagName('img')->item(1);
$imgC = $document->getElementsByTagName('img')->item(2);
$imgD = $document->getElementsByTagName('img')->item(3);
$imgA->setAttribute('loading', 'eager');
$imgB->setAttribute('loading', 'eager');
$imgC->setAttribute('loading', 'eager');
$imgD->setAttribute('loading', 'eager');
$html = $document->saveHTML($document->documentElement);
//$html = $document->saveHTML();
return $html;
} else {
return $content;
}
}
add_filter ('the_content', 'add_responsive_class', 999);
IT′S WORKING!!!
For posts, the first image, the code:
function add_responsive_class_2($content){
if (is_single()) {
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$document = new DOMDocument();
libxml_use_internal_errors(true);
$document->loadHTML(utf8_decode($content));
$imgA = $document->getElementsByTagName('img')->item(0);
//$imgA->setAttribute('loading', 'eager');
$imgA->removeAttribute('loading');
$html = $document->saveHTML($document->documentElement);
//$html = $document->saveHTML();
return $html;
} else {
return $content;
}
}
add_filter ('the_content', 'add_responsive_class_2', 998);
I will be testing all the functions, but it looks like that all is ok.
If you have some suggestion, please say.
Thanks and Best Regards,
Tony