我使用簡碼列出產品類別像這樣如何按特色或標籤對WooCommerce產品進行排序而無需額外插件?
[product_category category="products" orderby="title" order="asc"]
,並想知道我怎麼能做出特色產品是在列表頂部?它與orderby parameter有關,但我不知道。
請不要提出第三方插件。
更多關於WooCommerce官方的信息docs。
我使用簡碼列出產品類別像這樣如何按特色或標籤對WooCommerce產品進行排序而無需額外插件?
[product_category category="products" orderby="title" order="asc"]
,並想知道我怎麼能做出特色產品是在列表頂部?它與orderby parameter有關,但我不知道。
請不要提出第三方插件。
更多關於WooCommerce官方的信息docs。
產品具有作爲
_featured
的自定義字段被設置爲yes
的特色產品和no
非特色產品。因此,您可以使用 使用woocommerce_shortcode_products_query
過濾器來覆蓋 WooCOmmerce簡碼。
add_filter('woocommerce_shortcode_products_query', 'wh_woocommerce_shortcode_products_orderby');
function wh_woocommerce_shortcode_products_orderby($args)
{
$standard_array = ['menu_order', 'title', 'date', 'rand', 'id'];
// print_r($args['orderby']);
if (isset($args['orderby']) && !in_array($args['orderby'], $standard_array))
{
$args['meta_key'] = '_featured';
$args['orderby'] = 'meta_value_num';
}
// print_r($args);
return $args;
}
代碼放在您的活動子主題(或主題)的function.php文件。或者也可以在任何插件php文件中使用。
用法
要列出產品形態功能非功能(即形式yes
到no
)
[product_category category="products" orderby="_featured" order="DESC"]
請注意:: 上面的代碼似乎是工作,但如果你的打印$args['orderby']
它沒有顯示_featured
鍵,所以我硬編碼,它在$args
。這不是一種標準的方式。
希望這會有所幫助!
你想要一個短代碼或'WP_Query'也可以嗎? –
我更喜歡shortcode。 – Cowboy