
在 WooCommerce 产品gallery图册模块启用 GIF 动图显示
在使用 WooCommerce 构建电商网站时,你可能会发现一个小细节:上传到产品图册的 GIF 动图在产品页面主图或缩略图中不会播放,而是只显示 GIF 的第一帧。这可能会影响你向顾客展示动态效果或产品细节的方式。
本文将介绍一个简单的方法,让你的 GIF 动图在产品页面上直接动起来,而不必等到用户点击打开放大图弹窗。
将以下代码添加到你主题的 functions.php 文件中(或通过子主题、自定义插件添加):
// 修复 GIF 主图动起来,同时保留缩略图功能 add_filter('woocommerce_single_product_image_thumbnail_html', 'enable_gif_animation_in_gallery', 10, 2); function enable_gif_animation_in_gallery($html, $attachment_id) { $mime_type = get_post_mime_type($attachment_id); if ($mime_type === 'image/gif') { $full_src = wp_get_attachment_url($attachment_id); $thumbnail_src = wp_get_attachment_image_url($attachment_id, 'woocommerce_gallery_thumbnail'); $html = '<div class="woocommerce-product-gallery__image" data-thumb="' . esc_url($thumbnail_src) . '">'; $html .= '<a href="' . esc_url($full_src) . '">'; $html .= '<img src="' . esc_url($full_src) . '" />'; $html .= '</a></div>'; } return $html; }