 
    Products
GG网络技术分享 2025-03-18 16:12 26
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:
How can I remove that unused empty div?
Thanks in advance.
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我正在尝试使用php模数制作Wordpress循环。 场景是在 div </ code>中包装每3个帖子。 我的下面的脚本工作得很好,除了我在最后找到一个未使用的空 div </ code>(来自inspect元素)。</ p> 
  $ loop_args = array(\\  n\'post_type\'=&gt;\'post\',\'post_status\'=&gt;\'publish\',
\'possages_per_page\'=&gt; 6 
); 
$ loop = new WP_Query($ loop_args);  
&lt;?php if($ loop-&gt; have_posts()):?&gt; 
&lt; div class =“row”&gt; 
&lt;?php $ count = 0;  ?&gt; 
&lt; div class =“col-md-6”&gt; 
&lt;?php while($ loop-&gt; have_posts()):$ loop-&gt; the_post();  $计数++;  ?&gt; 
&lt;?php if($ count%3 == 0):?&gt; 
&lt; article class =“post-content”&gt;&lt;?php the_title();?&gt;&lt;  / article&gt; 
&lt; / div&gt;  &lt;! -  .col md 6  - &gt; 
&lt; div class =“col-md-6”&gt; 
&lt;?php else:?&gt; 
&lt; article class =“post-  content“&gt;&lt;?php the_title();?&gt;&lt; / article&gt; 
&lt;?php endif;  // end modulus?&gt; 
&lt;?php endwhile;  //结束循环?&gt; 
&lt; / div&gt;  &lt;! -  .col md 6  - &gt; 
&lt; / div&gt;  &lt;! -  .row  - &gt; 
&lt;?php endif;  wp_reset_postdata();  ?&gt; 
</ code> </ pre> 
请查看我的附件中最后突出显示的 div </ code>:</ p> 
 截图预览 </ p> 
如何删除 那个未使用的空 div </ code>?</ p> 
提前致谢。</ p> 
</ div> 
网友观点:
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.
    ###
The problem I see is that you are allways opeining the <div class=\\\"col-md-6\\\"> when the modulus 3 is 0 even if there are no more articles after you open the tag. In such case you would end up  with an empty div.
    
                    Demand feedback