2

我正在处理 WordPress 循环 - 输出是一个 4-col 容器,其中每个 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; ?>

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

谢谢 :)

4

3 回答 3

2

干得好

$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; ?>
于 2013-06-16T14:21:57.427 回答
0
<?php
if ($i == 4) {
    $i = 1;
} else {
    $i++;
}
?>
于 2013-06-16T14:23:40.620 回答
0

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

于 2013-06-16T14:24:32.897 回答