在 WooCommerce 子类别存档页面上显示子子类别术语列表

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

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

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



白板的微信
浏览 77回答 1
1回答

Smart猫小萌

要仅在主类别存档页面上显示第一个子类别,请仅使用:add_action('woocommerce_before_shop_loop', 'display_sub_subcategories', 10 );function display_sub_subcategories() {&nbsp; &nbsp; $obj&nbsp; &nbsp; &nbsp; = get_queried_object();&nbsp; &nbsp; $taxonomy = 'product_cat';&nbsp; &nbsp; if ( is_a($obj, 'WP_Term') && $taxonomy === $obj->taxonomy && 0 != $obj->parent ) {&nbsp; &nbsp; &nbsp; &nbsp; // Get sub-subcategories of the current subcategory&nbsp; &nbsp; &nbsp; &nbsp; $terms = get_terms([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy'&nbsp; &nbsp; => $taxonomy,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hide_empty'&nbsp; => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'parent'&nbsp; &nbsp; &nbsp; => $obj->term_id&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; if ( ! empty($terms) ) :&nbsp; &nbsp; &nbsp; &nbsp; $output = '<ul class="subcategories-list">';&nbsp; &nbsp; &nbsp; &nbsp; // Loop through product subcategories WP_Term Objects&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $terms as $term ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $term_link = get_term_link( $term, $taxonomy );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= '<li class="'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></li>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo $output . '</ul>';&nbsp; &nbsp; &nbsp; &nbsp; endif;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP