在while循环中每隔6个将<li>分组为<ul>

时间:2019-09-12 18:44:41

标签: php

我在WordPress网站上有一个“ while”语句,其中包含最新帖子。我想按顺序将每6个帖子分组。每6项是否可以将<li>元素包装在<ul>中?

我还没有尝试做任何事情,但不确定是否可能。我的下面的代码是。

<?php elseif(get_row_layout() == 'posts_section'): ?>
<section class="content posts">
    <div class="row">
        <div class="columns medium-12">
            <ul class="medium-block-grid-3 small-block-grid-2" data-equalizer>
                <?php
                    $catquery = new WP_Query('cat='. get_sub_field('category') .'');
                ?>

                <?php
                    while($catquery->have_posts()) : $catquery->the_post();
                ?>
                        <li data-equalizer-watch>
                            <a href="<?php the_permalink() ?>" rel="bookmark">
                                <div class="overlay"></div>

                                <div class="content">
                                    <p><?php the_title(); ?></p>
                                    <div class="text-center"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></div>
                                </div>
                            </a>
                        </li>
                <?php
                    endwhile;
                    wp_reset_postdata();
                ?>
            </ul>
        </div>
    </div>
</section>

1 个答案:

答案 0 :(得分:0)

由于您未提供任何其他信息,因此我无法测试以下代码,但这是我可以解决的方法:

<?php elseif(get_row_layout() == 'posts_section'): ?>
    <section class="content posts">
        <div class="row">
            <div class="columns medium-12">
                <ul class="medium-block-grid-3 small-block-grid-2" data-equalizer>
                    <?php
                        $catquery = new WP_Query('cat='. get_sub_field('category') .'');
                    ?>

                    <?php
                        $n = 0; //setting a count 
                    while($catquery->have_posts()) : $catquery->the_post();
                    if ($n % 6 != 0) {   
                        $n += 1 //keep on increasing till its divisble by 6
                        ?>
                            <li data-equalizer-watch>
                                <a href="<?php the_permalink() ?>" rel="bookmark">
                                    <div class="overlay"></div>

                                    <div class="content">
                                        <p><?php the_title(); ?></p>
                                        <div class="text-center"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></div>
                                    </div>
                                </a>
                            </li>
                    <?php
                      }
                       if ($n % 6 = 0) {
                       ?>
                        //end the ul tag here and start a new one
                         </ul>
                         <ul class="medium-block-grid-3 small-block-grid-2" data-equalizer>
                     <?php
                        }  
                        endwhile;
                        wp_reset_postdata();
                    ?>
               </ul>
            </div>
        </div>
    </section>