dongxingguo1978 2016-04-21 02:05
浏览 37

在Wordpress中显示帖子的类别和子类别的列表

This seems to be very simple but I don't know why the below code is not working. I have searched all over in Google, there are many solutions but not working for me. Guys, please let me know what I am missing.

My Code below:

    <ul class="category-sidebar">   
        <?php 
        $get_parent_cats = array(
        'parent' => '0' //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 //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><!--end of category-sidebar-->

This is only giving me Categories but not sub-categories in them. Please help anyone.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doutang6130 2016-04-22 09:15
    关注

    Hey I found the solution:

    <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>
    

    Thanks everyone for your time. :)

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效