Php 在Woocommerce存档中获取当前产品类别的子类别

Php 在Woocommerce存档中获取当前产品类别的子类别,php,wordpress,woocommerce,custom-taxonomy,taxonomy-terms,Php,Wordpress,Woocommerce,Custom Taxonomy,Taxonomy Terms,我试图在Woocommerce中显示当前类别下的子类别(而不是子类别等),如以下Web: 例如,建筑是一个类别,密封剂和粘合剂、防水材料、聚赖氨酸泡沫……是一个子类别 密封剂和乳化剂是这一类,醋酸硅酮密封剂、中性硅酮密封剂、丙烯酸密封剂……是这一类的子类别 在my child主题下的woocommerce文件夹中已经有一个archive-product.php 已经尝试了一些代码,但它适用于我,但这不是我想要的。以下代码将显示当前产品类别中用于产品类别存档页面的格式化链接产品子类别: if (

我试图在Woocommerce中显示当前类别下的子类别(而不是子类别等),如以下Web:

例如,建筑是一个类别,密封剂和粘合剂、防水材料、聚赖氨酸泡沫……是一个子类别

密封剂和乳化剂是这一类,醋酸硅酮密封剂、中性硅酮密封剂、丙烯酸密封剂……是这一类的子类别

在my child主题下的woocommerce文件夹中已经有一个archive-product.php


已经尝试了一些代码,但它适用于我,但这不是我想要的。

以下代码将显示当前产品类别中用于产品类别存档页面的格式化链接产品子类别:

if ( is_product_category() ) {

    $term_id  = get_queried_object_id();
    $taxonomy = 'product_cat';

    // Get subcategories of the current category
    $terms    = get_terms([
        'taxonomy'    => $taxonomy,
        'hide_empty'  => true,
        'parent'      => get_queried_object_id()
    ]);

    $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>';
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作


要在类别描述后显示,请在以下位置将挂钩优先级从
5
更改为
20

add_action('woocommerce_archive_description', 'display_subcategories_list', 5 ); 
比如:


以下代码将显示当前产品类别中用于产品类别存档页面的格式化链接产品子类别:

if ( is_product_category() ) {

    $term_id  = get_queried_object_id();
    $taxonomy = 'product_cat';

    // Get subcategories of the current category
    $terms    = get_terms([
        'taxonomy'    => $taxonomy,
        'hide_empty'  => true,
        'parent'      => get_queried_object_id()
    ]);

    $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>';
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作


要在类别描述后显示,请在以下位置将挂钩优先级从
5
更改为
20

add_action('woocommerce_archive_description', 'display_subcategories_list', 5 ); 
比如:


我需要把它放在哪里?在function.php中,那么:}elseif(!woocommerce\u product\u子类别(数组('before'=>woocommerce\u product\u loop\u start(false),'before'=>woocommerce\u product\u loop\u end(false)))呢{in archive-product.php?您好,首先,非常感谢您的回答和帮助。但这不是我想要的。我应该解释得最好。请再次参阅OP。是的,您是对的。我在解释我想要的内容时出错了。我是否应该像以前一样打开新问题并让它继续?再次感谢!我需要将它放在哪里?在function.php中?以及:}elseif中(!woocommerce_产品_子类别(数组('before'=>woocommerce_产品_循环_开始(false),'before'=>woocommerce_产品_循环_结束(false))){in archive-product.php?您好,首先,非常感谢您的回答和帮助。但这不是我想要的。我应该解释得最好。请再次参阅OP。是的,您是对的。我在解释我想要的内容时出错了。我应该像以前一样打开新问题并让它继续吗?再次感谢!