已存在对应尺寸,wp_get_attachment_image_src为何返回错误尺寸?
wp_get_attachment_image_src Returning Wrong Size (alm-thumbnail Instead of shop_thumbnail) Hey there! Let’s dig into why you’re encountering this frustrating issue—even though your shop_thumbnail size is registered, validated with Regenerate Thumbnails, and the corresponding 150x62 file exists, wp_get_attachment_image_src is spitting out alm-thumbnail instead. Here are the most likely culprits and how to test them:
1. Plugin Filter Overrides (Most Likely: Ajax Load More)
The alm-thumbnail prefix strongly points to Ajax Load More (ALM) or a related plugin. Many plugins (especially those handling lazy loading or dynamic content) hook into WordPress image functions to override requested sizes. ALM, for example, has settings that can force a specific thumbnail size across its queries, or it might have a custom filter that’s intercepting your shop_thumbnail request.
- Quick Test: Temporarily deactivate Ajax Load More (or any plugin with "alm" in its name) and re-run
wp_get_attachment_image_src. If it returns the correctshop_thumbnailsize, you’ve found the culprit. - Fix Steps:
- Check ALM’s settings panel for a "Thumbnail Size" option—make sure it’s not set to
alm-thumbnailglobally, or adjust it to respect your custom size for specific queries. - Look for custom code in your theme’s
functions.phpor plugin files that uses filters likewp_get_attachment_image_srcorimage_downsizeto override sizes. For example:add_filter('wp_get_attachment_image_src', 'alm_override_thumbnail', 10, 4); function alm_override_thumbnail($image, $attachment_id, $size, $icon) { // This might be replacing your $size with 'alm-thumbnail' return $image; }
- Check ALM’s settings panel for a "Thumbnail Size" option—make sure it’s not set to
2. Size Registration Conflicts or Metadata Mismatches
Even if you registered shop_thumbnail correctly, there could be a conflict with an existing size alias, or a mismatch between the attachment metadata and the actual file.
- Check Registration: Verify your
add_image_sizecode is correct (double-check dimensions and crop settings):add_image_size('shop_thumbnail', 150, 62, true); // True = hard crop, adjust if needed - Validate Metadata: Run
wp_get_attachment_metadata($attachment_id)and inspect thesizesarray. Confirm theshop_thumbnailentry has the correctfilename,width,height, and that the file exists at the specified path. If the metadata looks off, regenerate it manually:$attachment_id = 123; // Replace with your attachment ID $file = get_attached_file($attachment_id); wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
3. WordPress Downsize Fallback Logic
WordPress’s image_downsize function (called internally by wp_get_attachment_image_src) will fall back to the closest available size if it can’t find or access the requested size. Even if the file exists, permissions issues or path mismatches can make WP think it’s unavailable.
- Test File Access: Directly paste the URL of your 150x62
shop_thumbnailfile into a browser. If it doesn’t load, check file permissions (set to644for files,755for folders) or fix any path mismatches (e.g., if you moved your uploads folder but didn’t update WordPress settings).
4. Caching Interference
Caching plugins (like WP Rocket, LiteSpeed Cache) or server-level caching can hold onto old image size data, even after you regenerated thumbnails.
- Fix: Clear all caches:
- Plugin-specific caches (e.g., WP Rocket’s "Clear Cache" button)
- Server/browser cache
- CDN cache (if you’re using one)
内容的提问来源于stack exchange,提问作者Radley Sustaire

