我只希望在/../shop頁面上列出來自兩個特定類別的產品。其餘所有內容將需要被搜索或使用我已經在網站上的按鈕找到。Woocommerce - 僅限特定類別的商品頁面產品列表
我已經嘗試了幾個不同的代碼片段,但似乎沒有做我想要的東西。
任何幫助將非常學徒, 非常感謝 劉易斯
我只希望在/../shop頁面上列出來自兩個特定類別的產品。其餘所有內容將需要被搜索或使用我已經在網站上的按鈕找到。Woocommerce - 僅限特定類別的商品頁面產品列表
我已經嘗試了幾個不同的代碼片段,但似乎沒有做我想要的東西。
任何幫助將非常學徒, 非常感謝 劉易斯
我知道你希望顯示僅在/shop
頁面正確的特定categries?因此,使用下面的代碼,改變product_cat你需要什麼是...
add_action('pre_get_posts','shop_filter_cat');
function shop_filter_cat($query) {
if (!is_admin() && is_post_type_archive('product') && $query->is_main_query()) {
$query->set('tax_query', array(
array ('taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'type-1'
)
)
);
}
}
它在WooCommerce頁解釋,你只需要將它改變您的需求。
https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/
在你的情況,改變操作員 「IN」 和
術語=>數組( '刀')
到
術語=>數組(「你的'cat-slug-1','your-cat-slug-2'),
add_action('pre_get_posts', 'custom_pre_get_posts_query');
function custom_pre_get_posts_query($q) {
if (! $q->is_main_query()) return;
if (! $q->is_post_type_archive()) return;
if (! is_admin() && is_shop()) {
$q->set('tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array('your-cat-slug-1' , 'your-cat-slug-2'), // Display products in the your-cat-slug-1/your-cat-slug-2 category on the shop page
'operator' => 'IN'
)));
}
remove_action('pre_get_posts', 'custom_pre_get_posts_query');
}
把這段代碼在你的Child-themes的function.php文件中。
小遲到回覆對不起,但我已經測試了這個片段,我認爲它做了我所需要的一切,然後意識到它不僅允許監聽/商店頁面內的某些產品類別,而且僅允許這些類別顯示在搜索。有沒有辦法只在/商店頁面上允許某些類別,但搜索仍會搜索所有產品。 –
我不確定,現在不能測試它。但是,如果 (!is_admin()&&!is_search()&& is_shop()){ 可能有效。 – mbg
你的代碼在哪裏?顯示你的代碼。 – Deep