Php 显示带有标题的子类别产品

Php 显示带有标题的子类别产品,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我将woocommerce产品的显示更改为在shop循环中显示为表,而不是列元素。当我进入“商店”页面时,它会显示列表中的所有产品。我无法以有组织的方式显示产品,将产品分为相应的子类别。提前谢谢 $args=array( /** * @snippet WooCommerce Show Product Subcategories * @compatible WooCommerce 3.4 */ add_action( 'woocommerce_after_shop_loop_

我将woocommerce产品的显示更改为在shop循环中显示为表,而不是列元素。当我进入“商店”页面时,它会显示列表中的所有产品。我无法以有组织的方式显示产品,将产品分为相应的子类别。提前谢谢

$args=array(
/**
* @snippet       WooCommerce Show Product Subcategories
* @compatible    WooCommerce 3.4
*/

add_action( 
'woocommerce_after_shop_loop_item_title','bbloomer_show_all_subcats', 2 );

function bbloomer_show_all_subcats() {

// Create array with product categories that belong to current product

$cats = get_the_terms( $post->ID, 'product_cat' );

if ( ! empty( $cats ) ) {

// Loop through the product categories...

    foreach ( $cats as $term ) {

                    // If parent cat ID = 116 echo subcat name...
        if( $term->parent == 116 ) { echo $term->name; }

    }

    }

  }
“分类法”=>“产品分类”, “hide_empty”=>false, “父项”=>0 ); $product\U cat=获取条款($args); foreach($product\U cat作为$parent\U product\U cat) { 如果($parent\u product\u cat->name!=“Company”){ ?>


    • 请阅读并编辑您的问题。谢谢!我终于弄明白了。我能够在一个循环中拉出所有类别和子类别的
        ,并链接到相应的页面。从那里我插入到循环中以显示当前类别的产品。
        $args = array(
            'taxonomy' => 'product_cat',
            'hide_empty' => false,
            'parent'   => 0
        );
        
        $product_cat = get_terms( $args );
        
        foreach ($product_cat as $parent_product_cat)
        {
            if ($parent_product_cat->name != 'Company'){
            ?>
        
            <ul>
                <li><h2><a href='<?= get_term_link($parent_product_cat->term_id) ?>'><?= $parent_product_cat->name ?></a></h2>
                    <hr align='left' width='50%'>
                    <ul>
        
                        <?php
                        $child_args = array(
                            'taxonomy' => 'product_cat',
                            'hide_empty' => false,
                            'parent'   => $parent_product_cat->term_id
                        );
        
                        $child_product_cats = get_terms( $child_args );
                        foreach ($child_product_cats as $child_product_cat)
                        { ?>
                            <li style='padding-left: 2%;'>
                                <h3><a href='<?= get_term_link($child_product_cat->term_id) ?>'><?= $child_product_cat->name?></a></h3>
                            </li>
                            <div style='margin-left: -8%;'>
                                <?php
                                echo do_shortcode("[products category='$child_product_cat->term_id']");
                                ?>
                            </div>
                            <?php
                        }
                        ?>
        
                    </ul>
                </li>
            </ul>