php - 在 WooCommerce 商店页面中隐藏类别

标签 php woocommerce

我一直在尝试从 SHOP 页面隐藏特定类别。我找到这段代码:

add_filter( '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;

    $q->set( 'tax_query', array(array(
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => array( 'CATEGORY TO HIDE' ),
        'operator' => 'NOT IN'
    )));

    remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

我已将此代码粘贴到我的主题 function.php 文件中,但我没有实现结果...

有人能帮帮我吗?

最佳答案

我知道这有点晚了,但我自己遇到了这个问题并用以下函数解决了它:

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

function get_subcategory_terms( $terms, $taxonomies, $args ) {

  $new_terms = array();

  // if a product category and on the shop page
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {

    foreach ( $terms as $key => $term ) {

      if ( ! in_array( $term->slug, array( '**CATEGORY-HERE**' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;
  }

  return $terms;
}

关于php - 在 WooCommerce 商店页面中隐藏类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13366764/

相关文章:

php - 在 WooCommerce 订阅续订订单中设置送货方式

php - laravel 5.7如何查看文件(使用多文件上传代码)以及如何编辑

PhpUnit 内联数据提供程序

php - 从 MySql 记录集向多个收件人发送电子邮件

php - 根据 Woocommerce 中的特定产品属性值更改购物车商品价格

api - RESTful API - WooCommerce REST API 请求因curl 失败

woocommerce - 显示 woocommerce 类别列表

php - 与 'reading initial communication packet' 处的 MySQL 服务器失去连接,系统错误 : 110

php - PHP中的@是什么意思?

php - 将相关产品类别描述附加到 Woocommerce 中的产品描述