【问题标题】:Exclude a specific term from product categories widget in Woocommerce从 Woocommerce 中的产品类别小部件中排除特定术语
【发布时间】:2018-07-21 16:50:04
【问题描述】:

我有一些未分类的产品,因为它们不属于任何特定的产品类别。

我想在我的网站上保留未分类组中的产品(当人们搜索它时,当我在首页上显示它时等),但我想从类别下拉列表等中隐藏实际的未分类选项卡,所以人们看不到它。

我试过这段代码,但没有运气:

 // Do not include this if already open!
/**
 * Code goes in theme functions.php.
 */
add_filter( 'woocommerce_product_subcategories_args', 'custom_woocommerce_product_subcategories_args' );
function custom_woocommerce_product_subcategories_args( $args ) {
  $args['exclude'] = get_option( 'default_product_cat' );
  return $args;
}

在这张图片中,您可以看到问题所在。上面写着“Ukategoriseret”,意思是未分类:

【问题讨论】:

  • 你的意思是小部件产品类别(而不是标签可能是)...

标签: php wordpress woocommerce widget categories


【解决方案1】:

我想get_option( 'default_product_cat' ) 是自定义的,因为在测试时我没有得到任何输出。所以你必须确保你得到了一个term ID。

我还想您是在谈论产品类别小部件。如果是这种情况,您将需要使用此挂钩之一,具体取决于显示选择的选项设置。

以下代码用于从产品类别小部件中排除任何一组产品类别术语 ID:

add_filter('woocommerce_product_categories_widget_dropdown_args', 'widget_product_categories_list_args', 10, 1);
add_filter('woocommerce_product_categories_widget_args', 'widget_product_categories_list_args', 10, 1);
function widget_product_categories_list_args( $args ) {
    
    $default_term_id = get_option( 'default_product_cat' );
    
    // Excluding: a term ID or coma separated term IDs
    $args['exclude'] = array( $default_term_id ); 

    return $args;
}

代码进入您的活动子主题(活动主题)的 function.php 文件中。

经过测试并且有效。

【讨论】:

  • Loic 一如既往地完美运行。谢谢!!
猜你喜欢
  • 2018-11-09
  • 2013-08-11
  • 1970-01-01
  • 2019-07-15
  • 2017-03-27
  • 1970-01-01
  • 2018-05-17
  • 2019-08-05
  • 2021-04-19
相关资源
最近更新 更多