简体   繁体   English

WordPress-每2个帖子换行

[英]Wordpress - Wrap every 2 posts

I have 2 columns blog and I want to wrap every 2 posts using row class, so i try: 我有2列博客,我想使用行类包装每2篇文章,所以我尝试:

<?php $counter = -1; ?>
<?php while ( have_posts() ) : the_post();?>
    <?php $counter++;?>
    <?php if ($counter % 2 == 0) : ?>
        <?php echo '<div class="row">'; ?>
    <?php endif; ?>
        <?php get_template_part( 'template-parts/two-columns', get_post_format() );?>
    <?php
    if ($counter % 2 != 0) : ?>
        <?php echo '</div>'; ?>
    <?php endif; ?>
<?php endwhile; ?>

but if I have 2n+1 posts on page I can't close div - this part of code doesn't work 但是,如果我在页面上有2n + 1个帖子,则无法关闭div-这部分代码不起作用

if ($counter % 2 != 0) : ?>
    <?php echo '</div>'; ?>
<?php endif; ?>

How to solve this problem? 如何解决这个问题呢?

You actually answered your questions already by yourself with this part of code doesn't work You want to open the <div> and close it on the same condition... 您实际上已经用this part of code doesn't work单独回答了问题,您想打开<div>并在相同条件下将其关闭...

<?php $counter = -1; ?>
<?php while ( have_posts() ) : the_post();?>
    <?php $counter++;?>
    <?php if ($counter % 2 == 0) : ?>
        <?php echo '<div class="row">'; ?>
    <?php endif; ?>
        <?php get_template_part( 'template-parts/two-columns', get_post_format() );?>
    <?php if ($counter % 2 == 0) : ?>
        <?php echo '</div>'; ?>
    <?php endif; ?>
<?php endwhile; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM