Php 随机化“商业”;“分类购物”;

Php 随机化“商业”;“分类购物”;,php,wordpress,random,woocommerce,hook,Php,Wordpress,Random,Woocommerce,Hook,我目前在一家WooCommerce网站上工作。我选择了“按类别购物”选项,当前首页显示前3个类别 而不是前三个类别,我想它显示3个随机类别 我可以在我的function.php(下面的代码)中添加一个自定义函数,以增加列出的类别数(到10个),但我无法让类别以随机顺序显示 add_filter('storefront_product_categories_shortcode_args','custom_storefront_category_per_page' ); // Category P

我目前在一家WooCommerce网站上工作。我选择了“按类别购物”选项,当前首页显示前3个类别

而不是前三个类别,我想它显示3个随机类别

我可以在我的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;
}

我已经厌倦了设置$args['orderby']=“rand”;没有运气。我猜这只适用于产品。我应该更改什么功能,使首页上的“按类别购物”部分列出3个随机类别,而不是AESC或DESC order中的3个类别?

如果orderby=rand不适用于您的案例,请尝试以下技巧

--首先,您需要获取产品在页面中显示的随机类别

--然后将其传递给短代码

$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"]'); ?>

这会出现在子主题的function.php中吗?示例中的$random_cat是函数还是列表?您好,您获得了$all_cat数组中的所有类别名称,然后您需要选择一个随机类别,并创建一个类别字符串,其中逗号分隔为$randomCat,并将其传递给短码。