从Woocommerce的产品类别小部件中排除特定术语 [英] Exclude a specific term from product categories widget in Woocommerce

查看:68
本文介绍了从Woocommerce的产品类别小部件中排除特定术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a few products that are uncategorized, because they do not belong to any specific product category.

我想将未归类的产品保留在我的网站上(当人们搜索时,当我在首页上显示时,等等),但是我想要从类别下拉菜单等中隐藏实际的未分类标签,所以人们看不到它。

I'd like to keep the products in the uncategorized group available on my website (when people search for it, when I show it on the frontpage etc), but I want to hide the actual uncategorized tab from the category dropdown etc, so people do not see it.

我尝试了这段代码,但是没有运气:

I tried this code, but without luck:

 // 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,表示未分类:

Here in this image you can see the issue. It says "Ukategoriseret", which means uncategorized:

推荐答案

我想 get_option('default_product_cat')是自定义的,就像在测试我没有得到任何输出时一样。因此,您必须确保获得它的术语ID。

I suppose that get_option( 'default_product_cat' ) is something custom as when testing that I don't get any output. So you have to be sure that you are getting a term ID with it.

我还想您正在谈论产品类别窗口小部件 。如果是这种情况,则需要根据显示的所选选项设置使用其中一个钩子。

I suppose also that you are talking about the product category widget. If it's the case you will need to use one of this hooks, depending on the display selected option settings.

下面的代码可排除任何产品类别术语ID集来自产品类别小部件:

The code bellow works to exclude any set of product categories term IDs from product category widget:

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文件中(活动主题)。

经过测试并有效。

这篇关于从Woocommerce的产品类别小部件中排除特定术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆