Disable Lazy Loading Images Using the_post_thumbnail() in WordPress

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:

  1. Access your WordPress site’s files using FTP or a file manager.
  2. Locate the functions.php file of your theme. Normally, it is found in the wp-content/themes/your-theme/ directory.
  3. Edit the functions.php file 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);

  1. 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.


Leave a comment