You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已存在对应尺寸,wp_get_attachment_image_src为何返回错误尺寸?

Troubleshooting 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 correct shop_thumbnail size, 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-thumbnail globally, or adjust it to respect your custom size for specific queries.
    • Look for custom code in your theme’s functions.php or plugin files that uses filters like wp_get_attachment_image_src or image_downsize to 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;
      }
      

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_size code 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 the sizes array. Confirm the shop_thumbnail entry has the correct file name, 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_thumbnail file into a browser. If it doesn’t load, check file permissions (set to 644 for files, 755 for 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:48:23