簡體   English   中英

如何在自定義訂單中顯示帶有簡碼的產品?

[英]How to display products with shortcode in custom order?

我正在使用 woocommerce 簡碼來顯示自定義產品循環,例如[products limit="-1" paginate="true" orderby="menu_order" columns="5" ids="85,11,1083,256,37,12,517,68,2577,104"] 但是有一個問題——它們顯示的是按標題排序的,但我需要它們按照它們在短代碼中的確切順序進行排序。 我在短代碼文檔中沒有找到這樣的選項。 有沒有辦法用短代碼實現它?

woocommerce_shortcode_products_query_results掛鈎在從數據庫中查詢產品之后但在頁面上顯示之前運行。 通過鈎入這個鈎子並修改結果的順序,可以按所需的順序顯示產品。

//Products shortcode custom order by id
add_filter('woocommerce_shortcode_products_query_results', 'bytflow_custom_products_shortcode_order', 10, 2);
function bytflow_custom_products_shortcode_order($results, $shortcode_products){
    $attributes = $shortcode_products->get_attributes();
    $order_by_arg = get_query_var('orderby');
    // Check if there are any ids specified and that user didn't change the order
    if (empty($order_by_arg) && ! empty( $attributes['ids'] ) ) {
        $ids = explode( ',', $attributes['ids'] );
        $ordered_results = array();
        foreach ($ids as $id){
            if(in_array($id, $results->ids)){
                $ordered_results[] = $id;
            }
        }
        $results->ids = $ordered_results;
    }
    return $results;
}

此代碼檢查短代碼中是否存在 ids 屬性,如果存在,則重新排列 $results->ids 數組中的產品以匹配屬性中指定的順序。 來源

找到的解決方案: orderby = "post__in"

來源: Woocommerce 產品短代碼 按 ID 順序排序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM