首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >显示包含帖子的类别/子类别

显示包含帖子的类别/子类别
EN

Stack Overflow用户
提问于 2017-06-15 15:35:00
回答 1查看 1.6K关注 0票数 0

基本上,正如标题所说,我希望获得类别和子类别的列表,然后是这些类别/子类别的帖子(带有链接)。

这是我试图实现的结构:

代码语言:javascript
复制
<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>

到目前为止,在阅读了我能找到的关于这个主题的一切之后,我有了以下代码:

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
    <?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的经验,在这方面我真的需要帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-15 17:30:33

这是对任何感兴趣的人的解决方案:

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44561434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档