最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get the first image gallery of a product in woocommerce in a loop

programmeradmin2浏览0评论

I want to display a product with a product image and when visitor hovers over that image, it will change to the first image from the product gallery. I am using this code to display the image gallery, but it displays all the images from product gallery. I just want the 1 image.

  <?php do_action( 'woocommerce_product_thumbnails' ); ?>

Anybody know how to resolve this problem? Really appreciate any ideas.

Regards

I want to display a product with a product image and when visitor hovers over that image, it will change to the first image from the product gallery. I am using this code to display the image gallery, but it displays all the images from product gallery. I just want the 1 image.

  <?php do_action( 'woocommerce_product_thumbnails' ); ?>

Anybody know how to resolve this problem? Really appreciate any ideas.

Regards

Share Improve this question edited Dec 18, 2018 at 19:18 RiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges asked Mar 8, 2016 at 2:27 MailmulahMailmulah 971 gold badge4 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

Along with the product thumbnail (I'm assuming you have this), what you need is a list (array) of the product images - WooCommerce has such methods, eg $product->get_gallery_attachment_ids().

You can grab the first ID in the array and use it to fetch the single image using wp_get_attachment_image(), or wp_get_attachment_url(), etc., then use that as an alternate source for the main (thumbnail) image.

Incidentally, the woocommerce_product_thumbnails call is outputting markup that you probably don't want to use. You'll need to either discard this or unhook functions from it to get the output you want.

2018 Update: Need to use get_gallery_image_ids(); instead. Then use wp_get_attachment_url() with the first ID in the array returned.

$product->get_gallery_image_ids();
发布评论

评论列表(0)

  1. 暂无评论