【问题标题】:Woocommerce - Shop page product listing only certain categoryWoocommerce - 商店页面产品仅列出特定类别
【发布时间】:2017-03-03 11:57:56
【问题描述】:

我只想在 /../shop 页面上列出两个特定类别的产品。其余所有内容都需要使用我在网站上已有的按钮进行搜索或找到。

我尝试了一些不同的代码 sn-ps,但似乎没有什么能达到我想要的效果。

任何帮助都会得到很多学徒, 非常感谢 刘易斯

【问题讨论】:

  • 您的代码在哪里。显示您的代码。

标签: php wordpress woocommerce categories product


【解决方案1】:

WooCommerce 页面上有说明,您只需根据需要进行更改。

https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/

在您的情况下,将运算符更改为“IN”和

terms => array('knives')

terms => 数组('your-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 functions.php 文件中。

【讨论】:

  • 有点迟回复抱歉,但我已经测试了这个 sn-p,我认为它正在做我需要的一切,然后意识到它不仅允许在 /shop 页面中收听某些产品类别,而且也只允许在搜索中显示这些类别。有没有办法只允许 /shop 页面上的某些类别,但搜索仍会搜索所有产品。
  • 我不确定,现在无法测试。但也许 if ( !is_admin() && !is_search() && is_shop() ) { 可能有效。
【解决方案2】:

我了解到您希望在 /shop 页面上仅显示特定类别,对吗?因此,使用下面的代码,将 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'
                                     )
                         )
           );   
        }
     }

【讨论】:

    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2017-10-09
    • 2019-12-14
    • 1970-01-01
    相关资源
    最近更新 更多