简体   繁体   中英

Woocommerce random images in category page

I have a category page on my wordpress / woocommerce site that lists all of the prouct categories and the number of products that are in that category.

I think this is generated by content-product.php

Currently a placeholder is displayed but I would like to change this to an image.

What I want to do, for each of the categories, is randomly select and display an image from that category instead of the placeholder.

I've search on StackOverlflow and all over the net but I can't find an answer. Could someone point me in the right direction please? I can then try and write some code and see how far I get.

Thanks in Advance.

Something like this should work:

  • Find what products are in that category
  • Find which of those products have an image and add the ids to an array
  • Find the size of the array and use the rand() function to pick a number.
  • Use the number that was picked and get the product ID from that position in the array
  • Once you have the id then use wp_get_attachment_image() to display the image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM