繁体   English   中英

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

[英]List of showing categories and sub-categories of post in Wordpress

这似乎很简单但我不知道为什么下面的代码不起作用。 我在Google搜索过,有很多解决方案,但不适合我。 伙计们,请让我知道我错过了什么。

我的代码如下:

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

这只是给了我类别而不是子类别。 请帮助任何人。

提前致谢。

嘿,我找到了解决方案:

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

谢谢大家的时间。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM