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

仅显示WooCommerce子产品类别

湛宜春
2023-03-14

我目前正在使用以下代码获取WooCommerce产品类别:

<?php
    $orderby = 'name';
    $order = 'asc';
    $hide_empty = false ;
    $cat_args = array(
        'orderby'    => $orderby,
        'order'      => $order,
        'hide_empty' => $hide_empty,
    );

    $product_categories = get_terms( 'product_cat', $cat_args );

    if( !empty($product_categories) ){
        echo '<ul>';
        foreach ($product_categories as $key => $category) {
            echo '<li>';
            echo '<a href="'.get_term_link($category).'" >';
            echo $category->name;
            echo '</a>';
            echo '<li>';
        }
        echo '</ul>';
    }
?>

当前显示所有类别,但我希望仅显示子类别。

例如,如果您在“类别1”页面上,则该页面应仅显示该类别中的所有子级。

我在这里看过很多例子,但都找不到适合我需要的东西。

共有3个答案

籍昱
2023-03-14

在主题函数中添加以下代码。php文件。

add_filter('get_child_category', 'get_child_cat', 10, 1);
function get_child_cat($term_id = 0){
    global $wpdb;
    $result = array('status' => 'fail');
    if($term_id != 0){
        $result = $wpdb->get_results( "select ".$wpdb->prefix."terms.term_id, ".$wpdb->prefix."terms.name, ".$wpdb->prefix."terms.slug, ".$wpdb->prefix."terms.term_group, ".$wpdb->prefix."term_taxonomy.term_taxonomy_id, ".$wpdb->prefix."term_taxonomy.taxonomy, ".$wpdb->prefix."term_taxonomy.description, ".$wpdb->prefix."term_taxonomy.parent, ".$wpdb->prefix."term_taxonomy.count from ".$wpdb->prefix."terms left join ".$wpdb->prefix."term_taxonomy on ".$wpdb->prefix."term_taxonomy.term_id = ".$wpdb->prefix."terms.term_id where ".$wpdb->prefix."term_taxonomy.parent = $term_id" );
    }
    return $result;
}

使用下面的代码获取子类别的数组输出。

$cat = apply_filters( 'get_child_category', $term_id ); // where $term_id is your parent category id.

注:此代码仅用于单级术语。根据您的要求更改代码。

邓鸿彩
2023-03-14

您可以将其添加到woocommerce/archive产品中。php文件

$queried_object = get_queried_object();
$parent = $queried_object->term_id;
$categories = get_term_children( $parent, 'product_cat' ); 
if ( $categories && ! is_wp_error( $category ) ) : 

echo '<ul>';

foreach($categories as $category) :

$term = get_term( $category, 'product_cat' );
echo '<li>';
echo '<a href="'.get_term_link($term).'" >';
echo $term->name;
echo '</a>';
echo '</li>';

endforeach;

echo '</ul>';

endif;

这只适用于您的档案。和儿童类别。

它还将输出大子类别。

希望这有帮助。

阮阳曦
2023-03-14

以下是一种仅获取按名称ASC排序的产品子类别链接列表的方法:

// The product category taxonomy
$taxonomy = 'product_cat';

// Get the parent categories IDs
$parent_cat_ids = get_terms( $taxonomy, array(
    'parent'     => 0,
    'hide_empty' => false,
    'fields'     => 'ids'
) );

// Get only "child" WP_Term Product categories
$subcategories = get_terms( $taxonomy, array(
    'exclude'     => $parent_cat_ids,
    'orderby'    => 'name',
    'order'      => 'asc',
    'hide_empty' => false,
) );

if( ! empty( $subcategories ) ){
    echo '<ul>';
    foreach ($subcategories as $subcategory) {
        echo '<li>
            <a href="'. get_term_link($subcategory) .'" >' . $subcategory->name.'</a>
        </li>';
    }
    echo '</ul>';
}

代码经过测试并工作

 类似资料:
  • 我需要一些关于Woocommerce类别显示选项的帮助。在Wordpress仪表板中,我按照以下链接全局设置类别显示选项<外观- 有三种显示选项可用。 展示产品 目前,选择了第三个选项,它显示子类别以及父类别和子类别下的所有产品。但是,我想排除子类别下的所有产品。换句话说,我只想在父类别下显示子类别和产品。 我在几个教程网站上找到了以下片段。代码完全按照我想要的方式工作,但它也禁用了管理产品搜索和

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

  • 我在一家Woocommerce商店工作,该商店的产品按父类别(如品牌、颜色、大小等)组织。这些顶级类别中的每一个都有许多子类别:例如,颜色的子类别包括红色、蓝色、粉色等。 客户希望在首页和归档页的网格/循环视图中,在每个产品缩略图下方显示每个产品的品牌和尺寸(产品是鞋)。 每个产品只有一个品牌,但可能有多种尺寸。 因此,本质上,我需要知道是否有一种方法可以只显示单个产品的特定子类别的标题,如下所示

  • 我正在尝试创建一个页面与木商产品类别在标签。 我的选项卡菜单正在工作,但我需要在每个选项卡内容区域中运行一个查询到相应的类别。 但当我单击每个选项卡时,选项卡内容显示了类别中不属于当前选项卡的所有帖子。我没有得到正在出现的问题,请帮助我解决问题 下面是我的代码: 问题是它显示每个类别的所有post。我粘在上面了..请帮帮我

  • 我想在单产品页面上显示运输类,在从可变产品中选择一个产品之后。例如:我有一辆自行车有三种颜色(红、蓝、绿): 自行车红有航运类“免费送货” 蓝色自行车的运输类别为“额外交付” 绿色自行车的运输等级为“正常交付” 我感谢你的任何想法。

  • 在这个论坛上也有过类似的问题,但是没有一个提供的代码起作用,也许是因为几次Woocommerce的更新。 谢谢你!