要通过类别迭代,和子类别您可以使用多个嵌入foreach循环是这样的:
$taxonomy = 'product_cat';
$categories = get_categories(array(
'taxonomy' => $taxonomy,
'orderby' => 'name',
'empty' => 0,
'parent' => 0,
));
echo "<div>";
foreach ($categories as $category) {
echo '<a href="' . get_term_link($category->slug, 'product_cat') . '">' . $category->name . ' (' . $category->term_id . ')</a><br>';
$subcategories = get_categories(array(
'taxonomy' => $taxonomy,
'orderby' => 'name',
'empty' => 0,
'parent' => $category->term_id,
));
foreach ($subcategories as $subcategory) {
echo ' — <a href="' . get_term_link($subcategory->slug, 'product_cat') . '">' . $subcategory->name . ' (' . $subcategory->term_id . ')</a><br>';
$subsubcats = get_categories(array(
"taxonomy" => $taxonomy,
'orderby' => 'name',
'empty' => 0,
'parent' => $subcategory->term_id,
));
foreach ($subsubcats as $subsubcat)
echo ' — — <a href="' . get_term_link($subsubcat->slug, 'product_cat') . '">' . $subsubcat->name . ' (' . $subsubcat->term_id . ')</a><br>';
}
}
echo "</div>";
走得更远比子类2级,将需要为每个附加级别的嵌入额外的foreach循环...
此代码已经过测试并可正常工作。
谢谢!它的工作 –