dqlhsm9820 2018-05-23 15:08
浏览 80
已采纳

如何在wordpress循环中删除未使用的div?

I am trying to make Wordpress loop with php modulus. The scenario is wrapping every 3 posts in a div. My script below is working perfectly, except I found an unused empty div at the last (from inspect element).

$loop_args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 6
);

$loop = new WP_Query( $loop_args );

<?php if( $loop->have_posts() ): ?>

<div class="row">
    <?php $count = 0; ?>
    <div class="col-md-6">
    <?php while( $loop->have_posts() ) : $loop->the_post(); $count++; ?>
        <?php if( $count % 3 == 0 ): ?>
        <article class="post-content"><?php the_title();?></article>
    </div> <!-- .col md 6 -->
    <div class="col-md-6">
    <?php else: ?>
        <article class="post-content"><?php the_title();?></article>
        <?php endif; // end modulus ?>
        <?php endwhile; // end while loop ?>
    </div> <!-- .col md 6 -->
</div> <!-- .row -->

<?php endif; wp_reset_postdata(); ?>

Please take a look at the last highlighted div in my attachement:

screenshot preview

How can I remove that unused empty div?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongyu7074 2018-05-23 15:18
    关注

    Try this:

    <div class="row">
        <?php $count = 0; ?>
        <div class="col-md-6">
            <?php while( $loop->have_posts() ) : $loop->the_post(); $count++; ?>
                <?php if( $count % 3 == 1 && $count > 1 ): ?>
                    </div> <!-- .col md 6 -->
                    <div class="col-md-6">
                        <article class="post-content"><?php the_title();?></article>
                <?php else: ?>
                    <article class="post-content"><?php the_title();?></article>
                <?php endif; // end modulus ?>
            <?php endwhile; // end while loop ?>
        </div> <!-- .col md 6 -->
    </div> <!-- .row -->
    

    Here I don't close div on the third article but I open it if I need on the fourth.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀