简体   繁体   English

随机选择WooCommerce“按类别购物”

[英]Randomize WooCommerce “Shop By Category”

I'm currently working on a WooCommerce webstore. 我目前在WooCommerce网络商店上工作。 I have the shop by category option selected and the front page currently shows the first 3 categories. 我选择了“按类别购物”选项,当前首页显示了前3个类别。

Instead of the first three categories I would like it to show 3 random categories. 除了前三个类别,我希望它显示三个随机类别。

I was able to add a custom function to my function.php (code below) to increase the number of categories listed (to 10) but I'm not able to have the categories show up in a random order. 我能够向我的function.php(以下代码)添加自定义函数,以增加列出的类别数量(至10),但是我无法以随机顺序显示类别。

add_filter('storefront_product_categories_shortcode_args','custom_storefront_category_per_page' );

// Category Products
function custom_storefront_category_per_page( $args ) {
    $args['number'] = 10;
    return $args;
}

I have tired setting $args['orderby'] = "rand"; 我已经厌倦了设置$ args ['orderby'] =“ rand”; with no luck. 没有运气。 Im guessing that only works for products. 我猜测这仅适用于产品。 What function should I be changing to have the "Shop By Category" section on the front page list 3 random categories instead of 3 categories in AESC or DESC order? 我应该更改什么功能以使首页上的“按类别购物”部分列出3个随机类别,而不是AESC或DESC顺序中的3个类别?

if orderby = rand is not working for your case please try below technique. 如果orderby = rand不适合您的情况,请尝试以下技术。

-- first you need to get the random category for products to display in the page. -首先,您需要获取要在页面上显示的产品的随机类别。

-- then pass it to the shortcode. -然后将其传递给简码。

$categories = get_terms( array(
    'taxonomy' => 'product_cat',
    'hide_empty' => true,
) );

$all_cat = array();

foreach( $categories as $cat ){

$all_cat[] = $cat->name; 

}

$random_cat // get and create random category with comma seperated. and pass it to the shortcode.
 $randomCat = "tshirt, shirt";

echo do_shortcode('[products limit="8" columns="4" category="$randomCat" cat_operator="AND"]'); ?>

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

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