要通過類別迭代,和子類別您可以使用多個嵌入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循環...
此代碼已經過測試並可正常工作。
謝謝!它的工作 –