我想在前端的所有woocommerce類的,像這樣的結果子類別:得到woocommerce類與子類
<ul>
<li><a href="">Link</a>
<ul>
<li><a href="">Submenu link</a></li>
</ul>
</li>
</ul>
這裏是我有(但它不是我想要的):
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories($args);
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories($args2);
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
}
}
}
}
?>
這段代碼顯示的類別和子類別,但子類不應該放在這裏,子類是像這樣的單獨鏈接:
<ul>
<li><a href="">link</a></li>
<li><a href="">Submenu link</a></li>
</ul>
您的代碼複製從這裏粘貼:http://stackoverflow.com/questions/21009516/get-categories-from-wordpress-woocommerce你嘗試過什麼到目前爲止,或者你在哪裏遇到問題? –
檢查了這一點 - http://rachievee.com/wp-tutorial-how-to-create-a-categories-and-sub-categories-menu-part-1/ –
也檢查一次 - http:// www .wpworking.com/hacks-2/new-hack-for-displays-a-category-subcategory-menu-on-word/ –