当前位置: 首页 > 知识库问答 >
问题:

从WooCommerce产品类别小部件中删除父类别和子类别

安经纶
2023-03-14

我有以下代码,用于从WooCommerce产品类别小部件中删除除管理员和批发客户(自定义用户级别)之外的所有用户的“批发类别”(通过slug)。

这非常好,但是它只删除了父类别,留下了所有的子类别。我如何也删除“批发类别”的子类别?

// Woocommerce - Removes category link from woocommerce product category widgets so they are not seen
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

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

  $new_terms = array();

  $category_to_check = get_term_by( 'slug', 'wholesale-category', 'product_cat' );

  // if a product category and on the shop page
  if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'wholesale_customer' ) || current_user_can( 'administrator' ) ) ) {

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

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

    }

    $terms = $new_terms;

  }

  return $terms;
}

谢啦

共有2个答案

董飞
2023-03-14

请检查以下代码将做的工作。您需要用页面SLUG替换您的页面SLUG,并用需要隐藏的类别的产品类别SLUG替换“woo”

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
 // to hide from shop page, replace is_page('YOUR_PAGE_SLUG') with is_shop()
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_page('YOUR_PAGE_SLUG') ) {
    foreach ( $terms as $key => $term ) {
      if ( ! in_array( $term->slug, array( 'woo' ) ) ) {
        $new_terms[] = $term;
      }
    }
    $terms = $new_terms;
  }
  return $terms;
}
朱承载
2023-03-14

试试这样的东西...

// Woocommerce - Removes category link from woocommerce product category widgets so they are not seen
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

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

  $category_to_check = get_term_by( 'slug', 'wholesale-category', 'product_cat' );
  if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'wholesale_customer' ) || current_user_can( 'administrator' ) ) ) {
    foreach ( $terms as $key => $term ) {
      if ( in_array( $term->slug, array( $category_to_check->slug ) ) || ( $category_to_check->term_id == $term->parent ) ) {
        unset($terms[$key]);
      }
    }
  }
  return $terms;
}
 类似资料:
  • 我正在边栏和页脚小部件区域使用woocommerce产品类别小部件。 我有两个家长类别。我想这些被显示,但不是可点击的链接。 你可以在这里看到这一页http://www.terrykirkwood.co.uk/w 有人能建议添加什么css来阻止这些链接被点击吗? 谢谢 以下是第一次出现的代码:

  • 我正在用WooCommerce建立一个商店,并使用WooCommerce产品类别小部件。我用子类别设置了许多产品类别。其中一个类别是“海报”,有几个子类别,如“星标”、“旅游”、“自然”... 默认情况下,WooCommerce只显示好的父类别。如果我点击一个类别"海报",我被重定向到"海报"类别存档页面,小部件显示所有"海报"子类别,它是完美的。 现在,如果我点击其中一个“海报”子类别,我将被重

  • 我正在使用内置的woocommerce类别小部件,目前它同时显示类别和子类别。 我通过以下代码排除了一个类别: 但是小部件仍然显示它的子类别。 链接:http://tithaty.com.br/?post_type=product 隐藏的类别是Coleções(我配置为父级),我想隐藏它的子类别,当前的和将来添加的子类别。 Colecao teste是一个子类别的例子。 有什么想法吗? 非常感谢。

  • 希望你能帮我解决这个问题。我有一个问题,一些产品在子类别404'ing当去那里的产品页面。但奇怪的是,我在谷歌上找不到的只是他们中的一些人在做这件事。 实例 SKU:产品1--类别:类别1 SKU:产品2--类别:类别2,子类别1 SKU:Product3--类别:类别2,子类别1 SKU:Product4--类别:类别2,子类别1 将显示Product1、Product2、Product4。带有

  • 我正在与woocommerce建立电子商务网站。我在父类别下划分了子类别。我上传每个子类别的产品,不仅选择子类别,还选择父类别。但是,当我单击父类别时,我看不到任何产品。我只能在子类别中看到。例如,我有“手表”

  • 我有一个名为“产品类型”的WooCommerce产品类别,我试图列出该类别下的所有类别,但不是这些类别的子类别。例如,我有: 产品类型 碳化物米尔斯 钓鱼工具 儿童类别 我希望它列出“电石磨坊”和“打捞工具”,但不是“儿童类别”。 这是我的代码: 但它仍然返回“儿童类别”。我不知道为什么将深度限制为“1”并将“include_children”设置为“false”不能解决这个问题。