WordPress PHP循环增加div ID并重新启动它



我正在制作一个WordPress循环-输出是一个4色容器,其中每个div都有一个数量递增的类,在本例中为".c-1, .c-2, .c-3和.c-4"

我已经弄清楚了如何将数字增加到4,但现在我想知道如何在第四次之后"重新启动"数字,以便类整数在下一行再次从1开始。

下面是目前为止的代码:
<?php
$args = array(
    'posts_per_page' => 16,
);
$work = new WP_Query($args);
$i = 1;
while ($work->have_posts()) : $work->the_post(); ?>
    <div class="c-<?php echo $i; ?>">
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('work'); ?></a>
    </div>
    <?php $i++; ?>
<?php endwhile; ?>

我希望你们能帮助我,伙计们!我真的很感谢任何关于这个问题的建议:)

谢谢你

给你

$i = 1;
while ($work->have_posts()) : $work->the_post(); ?>
    //Reset $i to 1 when the counter reach the count of 4
    <?php if($i == 4) $i = 1; ?>
    <div class="c-<?php echo $i; ?>">
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('work'); ?></a>
    </div>
    <?php $i++; ?>
<?php endwhile; ?>
<?php
if ($i == 4) {
    $i = 1;
} else {
    $i++;
}
?>

$i++;替换为$i = $i % 4 + 1;

相关内容

最新更新