尝试从商店页面中删除几个类别

时间:2018-09-28 作者:Earrame

我尝试了几个狙击手,从的商店页面中删除了两个类别https://ornaments.com

function exclude_brands_from_related( $categories ){
    // array of category id\'s that should be excluded
    $exclude_cats = array( \'4792\', \'2486\');

    foreach( $categories as $index => $cat ){
        if( in_array( $cat->term_id, $exclude_cats ) ){
            unset($categories[$index]);
        }
    }

    return $categories;
}

add_filter( \'woocommerce_get_related_product_cat_terms\', \'exclude_brands_from_related\' );
以及:

 add_action( \'pre_get_posts\', \'custom_pre_get_posts_query\' );

function custom_pre_get_posts_query( $q ) {



    if ( ! is_admin() && is_shop() && ! is_user_logged_in() ) {

        $q->set( \'tax_query\', array(array(
            \'taxonomy\' => \'product_cat\',
            \'field\' => \'slug\',
            \'terms\' => array( \'ethnic-ornaments\',\'uncategorized\' ), // Don\'t display products in these categories on the shop page
            \'operator\' => \'NOT IN\'
        )));

    }

    remove_action( \'pre_get_posts\', \'custom_pre_get_posts_query\' );

}
我正在使用插件代码Snipits来实现代码,但我也尝试将其直接放在函数中。php文件无效。

任何帮助都将不胜感激!

1 个回复
SO网友:Tung Du

要更改商店页面上的产品,您需要woocommerce_product_query 措施:

/**
 * Exclude products from a particular category on the shop page
 */
function custom_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( \'tax_query\' );

    $tax_query[] = array(
       \'taxonomy\' => \'product_cat\',
       \'field\' => \'slug\',
       \'terms\' => array( \'clothing\' ), // Don\'t display products in the clothing category on the shop page.
       \'operator\' => \'NOT IN\'
    );


    $q->set( \'tax_query\', $tax_query );

}
add_action( \'woocommerce_product_query\', \'custom_pre_get_posts_query\' );

结束