如何显示同一类别的多个帖子?

How to show multiple posts of a same category?

我想显示同一类别的多个帖子,但我只想打印类别名称 once.The 具有相同类别的帖子将在一次打印的类别名称下一起打印。 下面是我想要的输出图片:

这是我的代码

<?php 
                            $args = array('post_type' => 'books_section', 'posts_per_page' => -1 );
                            $the_query = new WP_Query($args);
                            while ($the_query -> have_posts()): $the_query -> the_post();
                        ?>
                        <div class="col-3 col-sm-3 col-md-2 col-lg-1 col-xl-1">
                            <div class="img-category w-75">
                                <?php 
                                $category = get_the_category(); 

                                if($category[0]){

                                    $thumbnail = the_post_thumbnail('thumbnail');

                                    echo "<img height='50' width='100' alt='' src='$thumbnail'>";
                                }
                                ?>
                                <!-- <img height="50" width="100" alt="" src="<?php the_field('category_image'); ?>"> -->
                            </div>
                        </div>
                        <div class="col-9 col-sm-9 col-md-10 col-lg-11 col-xl-11">
                            <h5 class="text-capitalize">
                                <?php
                                    $category = get_the_category();
                                    echo $category[0]->cat_name;
                                ?>
                            </h5>
                        </div>
                        <div class="col-12 col-sm-12 col-md-6 col-lg-3 col-xl-3 mt-5">
                            <a class="text-decoration-none" href="<?php the_field('amazon_book_link');?>" target="">
                                <div class="book-hover">
                                <?php the_post_thumbnail('full'); ?>
                                    <!-- <img alt="" src="./assets/images/Layer 40@1X.png" class="" height="350" width="250"> -->
                                </div>
                                <h6 class="text-capitalize font-weight-bold text-center mt-2 book-title"><?php the_title(); ?></h6>
                                <?php if( get_field('audio_book') == 'yes' ): ?>
                                    <h6 class="text-capitalize font-weight-bold text-center mt-2 book-type">Audio Book</h6>
                                    <?php endif; ?>
                                <?php if( get_field('comng_soon') == 'yes'): ?>
                                    <h6 class="text-capitalize font-weight-bold text-center mt-2 book-type">Coming Soon</h6>
                                <?php endif; ?>
                            </a>
                        </div>
                        <?php endwhile; ?>

这是我得到的结果:

我现在得到的结果是循环正在分别打印所有帖子及其类别名称,但我只需要一次类别名称并打印该特定类别的所有帖子。 然后循环应该打印新类别的其他帖子。 请帮助我。

This 准确回答您的问题。

在示例中他设置了 5 posts 的限制,您可以将其设置为 -1 以表示无限制的 posts。

此外,如果您想隐藏没有任何 post 的类别,只需将其添加到 get_categories() 的 $args:

'hide_empty' => true

希望对您有所帮助!

注意:如果您对电影使用自定义 post,您还应该创建自定义分类法而不是使用默认类别。仅当您完全确定您永远不会将它们用于普通文章时才使用类别。

献上我的回答的各位,特别是初学者的我,不是一件容易的事。因此,我进行了试验并找到了解决问题的方法。这是完美运行的代码。

<?php 
    $args = array(
               'orderby' => 'name',
               'hide_empty'=> true,
                 );
    $categories = get_categories($args);
    foreach ($categories as $cat) {
?>
<h5><?php echo $cat->name; ?></h5>
<?php
    $args = array('post_type' => 'books_section', 'posts_per_page' => -1, 'category_name' => $cat->name);
    $the_query = new WP_Query($args);
    while ($the_query -> have_posts()): $the_query -> the_post();   
?>
//Rest of the HTML goes here to style books
<?php endwhile;?>
<?php 
   }
?>

我在这段代码中所做的是,首先我启动了一个 foreach 循环来一个一个地获取所有类别,然后在这个循环(嵌套循环)中通过另外添加一个类别名称在 while 循环中获取我的所有帖子参数,因此只会获取那些具有上述由 foreach 循环生成的类别名称的帖子。因此,当外层循环第一次获取类别时,它将移至 while 循环内并获取与该类别相关的所有帖子,然后离开内层循环。这个过程周而复始,最后我根据他们的类别得到了我所有的帖子。