使用以下代码,我可以列出Woocommerce产品类别中的所有顶级类别
$args = array(
\'number\' => $number,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'hide_empty\' => $hide_empty,
\'parent\' => 0,
\'include\' => $ids
);
$product_categories = get_terms( \'product_cat\', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo \'<h4><a href="\' . get_term_link( $product_category ) . \'">\' . $product_category->name . \'</a></h4>\';
}
}
它们是:
隐形眼镜
设计师眼镜
设计师太阳镜
眼镜镜片
现在我需要列出所有子类别Designer Sunglasses
在另一页中,我试图通过designer-sunglasses
slug代替\'product_cat\'
在get_terms()
但它没有返回任何东西。你能告诉我怎么做,并在单独的页面中列出每个子类别的所有子类别吗?
使现代化
按ID:
$args = array(
\'number\' => $number,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'hide_empty\' => $hide_empty,
\'parent\' => 1702,
\'include\' => $ids
);
$product_categories = get_terms( \'product_cat\', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo \'<h4><a href="\' . get_term_link( $product_category ) . \'">\' . $product_category->name . \'</a></h4>\';
}
}
按名称:
$args = array(
\'number\' => $number,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'hide_empty\' => $hide_empty,
\'parent\' => "designer-sunglasses",
\'include\' => $ids
);
$product_categories = get_terms( \'product_cat\', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo \'<h4><a href="\' . get_term_link( $product_category ) . \'">\' . $product_category->name . \'</a></h4>\';
}
}