为WooCommerce Product Feed Plugin PRO添加XML Feed自定义缩略图属性
Hey there, I’ve run into this exact issue before—great question! The plugin defaults to main images, but we can fix this either by adding a custom product attribute that pulls thumbnails, or using a filter hook to directly override the image in the feed. Let’s walk through both options:
Option 1: Add a Custom Attribute for Thumbnails
This method creates a reusable custom attribute you can select directly in the plugin’s feed editor.
Add the custom attribute code
Paste this into your theme’sfunctions.phpfile (or use a custom plugin to avoid losing changes when updating your theme):// Register a custom product attribute for thumbnails add_filter('woocommerce_product_attributes', 'register_thumbnail_attribute'); function register_thumbnail_attribute($attributes) { $attributes['product_thumbnail'] = array( 'label' => __('Product Thumbnail', 'woocommerce'), 'value' => '', 'is_visible' => false, // Keep this hidden on product pages 'is_variation' => false, 'is_taxonomy' => false ); return $attributes; } // Populate the attribute with the thumbnail URL for each product add_action('woocommerce_save_product', 'populate_thumbnail_attribute_value'); function populate_thumbnail_attribute_value($product_id) { $product = wc_get_product($product_id); $thumbnail_id = $product->get_image_id(); // Use 'thumbnail' for default size, or replace with your custom size slug (e.g., 'medium') $thumbnail_url = wp_get_attachment_image_url($thumbnail_id, 'thumbnail'); if ($thumbnail_url) { update_post_meta($product_id, 'product_thumbnail', $thumbnail_url); } }Configure the feed plugin
- Open your XML feed in the WooCommerce Product Feed Plugin PRO editor.
- Find the field that’s currently pulling the main product image (look for labels like
Product ImageorMain Image). - Replace it with a Custom Field type, and enter the meta key
product_thumbnail. - Save your feed settings, then regenerate the feed to apply changes.
Option 2: Directly Override the Image via Filter Hook
If you don’t want to add a custom attribute, this hook will automatically swap the main image with the thumbnail in your feed:
Add the filter code
Paste this into your theme’sfunctions.phpor a custom plugin:add_filter('woo_feed_product_image', 'swap_main_image_with_thumbnail', 10, 2); function swap_main_image_with_thumbnail($original_image_url, $product) { $thumbnail_id = $product->get_image_id(); $thumbnail_url = wp_get_attachment_image_url($thumbnail_id, 'thumbnail'); // Fall back to original main image if no thumbnail exists return $thumbnail_url ? $thumbnail_url : $original_image_url; }Regenerate your feed
No extra plugin configuration needed—just regenerate your XML feed, and the thumbnails will replace the main images automatically.
Notes
- If you use custom image sizes in WooCommerce, replace
'thumbnail'in the code with your custom size slug (e.g.,'custom-thumbnail'). - Always backup your
functions.phpfile before adding code, or use a code snippets plugin to safely add custom code.
内容的提问来源于stack exchange,提问作者Hamed mayahian

