显示带有帖子WP的类别/子类别

时间:2017-06-15 07:35:00

标签: wordpress

基本上,标题说我想获得类别和子类别列表,然后发布这些类别/子类别的帖子(带有链接)。

这是我想要实现的结构:

<ul>
  <li>Category 1</li>
    <ul>
      <li>Subcategory 1 within category 1</li>
        <ul>
          <li>Post 1 within subcategory 1</li>
          <li>Post 2 within subcategory 1</li>
          <li>Post 3 within subcategory 1</li>
        </ul>
      <li>Subcategory 2 within category 1</li>
        <ul>
          <li>Post 1 within subcategory 2</li>
          <li>Post 2 within subcategory 2</li>
          <li>Post 3 within subcategory 2</li>
        </ul>
      <li>Subcategory 3 within category 1</li>
        <ul>
          <li>Post 1 within subcategory 3</li>
          <li>Post 2 within subcategory 3</li>
          <li>Post 3 within subcategory 3</li>
        </ul>
      <li>Posts that have no subcategory</li>
        <ul>
          <li>Post 1 with no subcategory</li>
          <li>Post 2 with no subcategory</li>
        </ul>
    </ul>
  <li>Category 2</li>
    <ul>
      <li>Subcategory 1 within category 2</li>
        <ul>
          <li>Post 1 within subcategory 1</li>
          <li>Post 2 within subcategory 1</li>
          <li>Post 3 within subcategory 1</li>
        </ul>
      <li>Subcategory 2 within category 2</li>
        <ul>
          <li>Post 1 within subcategory 2</li>
          <li>Post 2 within subcategory 2</li>
          <li>Post 3 within subcategory 2</li>
        </ul>
      <li>Subcategory 3 within category 2</li>
        <ul>
          <li>Post 1 within subcategory 2</li>
          <li>Post 2 within subcategory 2</li>
          <li>Post 3 within subcategory 2</li>
        </ul>
      <li>Posts that have no subcategory</li>
        <ul>
          <li>Post 1 with no subcategory</li>
          <li>Post 2 with no subcategory</li>
        </ul>
    </ul>
</ul>

现在,在阅读了我在该主题上找到的所有内容后,到目前为止,我有以下代码:

<ul>   
    <?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
            );

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

现在这段代码很好地展示了类别和子类别,但我需要以某种方式遍历我的帖子并用类别/子类别显示它们。 我也试过使用fallowing代码:                      

    <?php
        // get all the categories from the database
        $cats = get_categories(); 

            // loop through the categries
            foreach ($cats as $cat) {
                // setup the cateogory ID
                $cat_id= $cat->term_id;
                // Make a header for the cateogry
                echo "<h2>".$cat->name."</h2>";
                // create a custom wordpress query
                query_posts("cat=$cat_id&posts_per_page=100");
                // start the wordpress loop!
                if (have_posts()) : while (have_posts()) : the_post(); ?>

                    <?php // create our link now that the post is setup ?>
                    <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
                    <?php echo '<hr/>'; ?>

                <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
            <?php } // done the foreach statement ?>

        </div><!-- #content -->
    </div><!-- #container -->

此代码显示特定类别中的所有类别和帖子,但结构不是我想要的结构。 我一直试图将这两段代码组合两天,但我尝试的任何东西都没有给我我想要的结果。 我对Wordpress缺乏经验,我真的可以帮忙解决这个问题。

1 个答案:

答案 0 :(得分:1)

This is the solution for anyone interested:

<ul>   
        <?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
                 echo '<ul class="post-title">';

                $query = new WP_Query( array( 'cat'=> $catID, 'posts_per_page'=>10 ) );
                while( $query->have_posts() ):$query->the_post();
                 echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';
                endwhile;
                wp_reset_postdata();

                echo '</ul>';
                $get_children_cats = array(
                    'child_of' => $catID //get children of this parent using the catID variable from earlier
                );

                $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 class="post-title">';

                        $query = new WP_Query( array( 'cat'=> $childID, 'posts_per_page'=>10 ) );
                        while( $query->have_posts() ):$query->the_post();
                         echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';
                        endwhile;
                        wp_reset_postdata();

                        echo '</ul>';

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