2016-04-21 289 views
1

這似乎很簡單,但我不知道爲什麼下面的代碼不起作用。我在谷歌全面搜索,有很多解決方案,但不爲我工作。夥計們,請讓我知道我錯過了什麼。在Wordpress中顯示帖子的類別和子類別列表

我下面的代碼:

這只是給我的類別而不是子類在其中。 請幫助任何人。

在此先感謝。

+0

爲什麼不使用['wp_list_categories()'](https://developer.wordpress.org/reference/functions/wp_list_categories/)? – MinhTri

+0

我也嘗試過,但得到同樣的東西.. :( –

+0

禁用所有插件,然後嘗試核心WordPress主題。確保子類別存在。 – MinhTri

回答

0

嘿,我找到了解決辦法:

<ul class="category-sidebar"> 
     <?php 
     $get_parent_cats = array(
     'parent' => '0','hide_empty' => false //get top level categories only 
     ); 

     $all_categories = get_categories($get_parent_cats);//get parent categories 

     foreach($all_categories as $single_category){ 
     //for each category, get the ID 
     $catID = $single_category->cat_ID; 

     echo '<li><a href=" ' . get_category_link($catID) . ' ">' . $single_category->name . '</a>'; //category name & link 
     $get_children_cats = array(
     'child_of' => $catID,'hide_empty' => false //get children of this parent using the catID variable from earlier 
     ); 

     $categories = get_categories($args); 

     $child_cats = get_categories($get_children_cats);//get children of parent category 
     echo '<ul class="children">'; 
     foreach($child_cats as $child_cat){ 
     //for each child category, get the ID 
     $childID = $child_cat->cat_ID; 

     //for each child category, give us the link and name 
     echo '<a href=" ' . get_category_link($childID) . ' ">' . $child_cat->name . '</a>'; 

     } 
     echo '</ul></li>'; 
     } //end of categories logic ?> 
     </ul> 

謝謝大家對你的時間。 :)