在 WooCommerce 子类别存档页面上显示子子类别术语列表 [英] Display sub subcategories terms list on WooCommerce subcategory archive pages

查看:38
本文介绍了在 WooCommerce 子类别存档页面上显示子子类别术语列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Woocommerce 中,我使用 获取Woocommerce存档中当前产品类别的子类别答案功能,在父类别页面上显示子类别列表

In Woocommerce I use Get the subcategories of the current product category in Woocommerce archives answer function, to display a list of subcategories on the parent category pages

但我只需要将其应用于特定的产品类别,并且使用带有类别 ID 预感的数组似乎并不理想.

But I only need to apply this to SPECIFIC product-categories though, and to use array with a hunch of category IDs seems not ideal.

我只需要在第一个子类别上显示列表,例如,我的主要父类别之一是服装",然后是子类别衬衫".然后是子子类别无袖".我只需要在第一个子类别中显示它,在本例中为衬衫".

I need to display the list only on the first children categories, so for example one of my main parent categories is "Clothing", then the subcategory "Shirts" and then the sub-sub-category "Sleeveless". I only need to display it on the first sub-categories, in this example "Shirts".

推荐答案

要仅显示主类别存档页面上的第一个子类别,请使用:

To display only the first subcategory on main category archive pages only use:

add_action('woocommerce_before_shop_loop', 'display_sub_subcategories', 10 );
function display_sub_subcategories() {
    $obj      = get_queried_object();
    $taxonomy = 'product_cat';

    if ( is_a($obj, 'WP_Term') && $taxonomy === $obj->taxonomy && 0 != $obj->parent ) {
        // Get sub-subcategories of the current subcategory
        $terms = get_terms([
            'taxonomy'    => $taxonomy,
            'hide_empty'  => true,
            'parent'      => $obj->term_id
        ]);

        if ( ! empty($terms) ) :

        $output = '<ul class="subcategories-list">';

        // Loop through product subcategories WP_Term Objects
        foreach ( $terms as $term ) {
            $term_link = get_term_link( $term, $taxonomy );

            $output .= '<li class="'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></li>';
        }

        echo $output . '</ul>';
        endif;
    }
}

这篇关于在 WooCommerce 子类别存档页面上显示子子类别术语列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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