WordPress教程:WooCommerce商店页面中删除某些产品类别

经过 小朱笔记 | 3 月 17, 2019 | WordPress技巧 | 0条评论

商店页面中删除某些产品类别

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() && ! is_user_logged_in() ) {
 
    $q->set( 'tax_query', array(array(
      'taxonomy' => 'product_cat',
      'field' => 'slug',
      'terms' => array( 'color', 'flavor', 'spices', 'vanilla' ), https:// Don't display products in these categories on the shop page
      'operator' => 'NOT IN'
    )));
 
  }
 
  remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
 
}

在哪里添加此代码?

将PHP代码放在主题或子主题functions.php文件的底部。

0条评论

提交评论

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理