【问题标题】:Display sub subcategories terms list on WooCommerce subcategory archive pages在 WooCommerce 子类别存档页面上显示子子类别术语列表
【发布时间】:2020-11-25 14:39:08
【问题描述】:

在 Woocommerce 中,我使用 Get the subcategories of the current product category in Woocommerce archives 回答功能,在父类别页面上显示子类别列表

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

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

【问题讨论】:

  • 你的意思是 - 你只需要在特定类别模板上显示它?
  • 我编辑了我的问题以澄清。

标签: php wordpress woocommerce hierarchical taxonomy-terms


【解决方案1】:

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

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;
    }
}

【讨论】:

  • 不,我需要在子类别页面上显示所有子子类别。例如,我有 hircharchy 父类别:服装,子类别:衬衫,然后是多个子子类别,如无袖、马球、上衣等。我需要在子类别页面上显示所有子子类别。所以无袖、马球、上衣应该显示在衬衫页面上。
  • @Arkansas4​​4 好吧,之前没听懂。更新了……试试看这是不是你想要的。如果这个答案回答了你的问题,你可以请accept回答,如果你喜欢/想要你也可以请upvote回答,谢谢。
  • 是的,这可行,但它也会将 html
      添加到子子类别页面。它不是显示在其中,而是将空 div 添加到这些档案中...
    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 2018-12-07
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    相关资源
    最近更新 更多