To disable lazy loading for images using the the_post_thumbnail() function in WordPress, you will need to modify your theme’s functions.php file. Here’s how:
- Access your WordPress site’s files using FTP or a file manager.
- Locate the
functions.phpfile of your theme. Normally, it is found in thewp-content/themes/your-theme/directory. - Edit the
functions.phpfile and add the following code at the end:
function disable_lazy_load_post_thumbnail($attr, $attachment, $size) {
$attr['loading'] = 'eager';
return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'disable_lazy_load_post_thumbnail', 10, 3);
- Save the file and upload it back to your server.
By adding this code, you are hooking into the wp_get_attachment_image_attributes filter and modifying the loading attribute of the the_post_thumbnail() images. Setting the loading attribute to 'eager' disables lazy loading for those images.
Once you’ve made these changes, lazy loading will be disabled for all the_post_thumbnail() images in your WordPress site. Remember to clear any caching plugins or server caches to see the changes take effect.
