Wordpress 如何在父/子类别模板中同时输出父类别和子类别

Wordpress 如何在父/子类别模板中同时输出父类别和子类别,wordpress,woocommerce,Wordpress,Woocommerce,我尝试在父/子类别模板(woocommerce)中显示父类别和子类别。 我使用下面的代码,它只是在父类别中显示子类别 更新代码: <?php $counter = 0; $term = get_queried_object(); $children = get_terms( $term->taxonomy, array( 'parent' => $term->t

我尝试在父/子类别模板(woocommerce)中显示父类别和子类别。 我使用下面的代码,它只是在父类别中显示子类别

更新代码:

<?php
            $counter = 0;
            $term = get_queried_object();

            $children = get_terms( $term->taxonomy, array(
                'parent'    => $term->term_id,
                'hide_empty' => false
            ) );

            $categories = [ $term ];

            // If $children is an array just merge the "current" (parent) category with the children
            if ( is_array( $children ) ) {
                $categories = array_merge( $categories, $children );
            }


            foreach( $categories as $subcat ) {
                    echo '<ul><li><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . $subcat->name . '</a></li></ul>';
            }
        ?>

在本例中,您查询的是所有“子项”,如果您试图创建一个包含所有已有数据的数组,您只需构建它即可

<?php
            $counter = 0;
            $term = get_queried_object();

            $children = get_terms( $term->taxonomy, array(
                'parent'    => $term->term_id,
                'hide_empty' => false
            ) );

            $categories = [ $term ];

            // If $children is an array just merge the "current" (parent) category with the children
            if ( is_array( $children ) ) {
                $categories = array_merge( $categories, $children );
            }


            foreach( $categories as $subcat ) {
                    echo '<ul><li><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . $subcat->name . '</a></li></ul>';
            }
        ?>


正如您在上面的示例中看到的,添加了一小部分代码以将两个数组合并为一个数组。

谢谢,那么如何在子类别模板中显示所有列表子类别和父类别?例如:-parent category--child category 01--child category 02--child category n的模板将包括-parent category,还包括其他子类别。