WordPress 网格 PHP

Wordpress Grid PHP

我在向我的项目添加网格布局时遇到问题,代码如下:

<?php while ( have_posts() ) : the_post(); ?>
    <?php
        $args = array(
            'post_type'      => 'projects',
            'posts_per_page' => 2,
        );
        $q    = new WP_Query( $args );
    ?>

    <div class="row">
        <?php while ( $q->have_posts() ) : $q->the_post(); ?>
            <div class="col-6">
                <h3>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                </h3>
                <?php the_excerpt(); ?>
            </div>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    </div>

<?php endwhile; ?>

它看起来像这样:The Image。只要我有帖子,它就会重复 2 列网格。

谁能帮我解决这个问题,让我的项目获得正常的网格布局?

谢谢!

从顶部删除 <?php while ( have_posts() ) : the_post(); ?>,从底部删除 <?php endwhile; ?>。因为您在该循环中有一个自定义 WP 查询。所以不需要默认的 Wordpress post 查询循环。