首页 > 解决方案 > 显示带有标题的子类别 WooCommerce 产品

问题描述

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

标签: phpwordpresswoocommerce

解决方案


/**
* @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; }

    }

    }

  }

推荐阅读