【问题标题】:Randomize WooCommerce "Shop By Category"随机化 WooCommerce“按类别购物”
【发布时间】:2018-05-18 15:54:56
【问题描述】:

我目前在 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 顺序排列的 3 个类别?

【问题讨论】:

    标签: php wordpress random woocommerce hook


    【解决方案1】:

    如果 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 并将其传递给简码。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 2019-08-17
    • 2015-12-09
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多