简体   繁体   English

Woocommerce类别页面中的随机图像

[英]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. 我在wordpress / woocommerce网站上有一个类别页面,其中列出了所有产品类别以及该类别中的产品数量。

I think this is generated by content-product.php 我认为这是由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. 我已经在StackOverlflow和整个网络上进行搜索,但是找不到答案。 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 查找其中哪些产品具有图片,然后将ID添加到数组中
  • Find the size of the array and use the rand() function to pick a number. 找到数组的大小,然后使用rand()函数选择一个数字。
  • Use the number that was picked and get the product ID from that position in the array 使用选择的编号,并从阵列中的该位置获取产品ID
  • Once you have the id then use wp_get_attachment_image() to display the image. 拥有ID后,请使用wp_get_attachment_image()显示图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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