【问题标题】:WordPress PHP loop increase div ID and restart itWordPress PHP循环增加div ID并重新启动它
【发布时间】:2013-06-16 14:16:51
【问题描述】:

我正在处理 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; ?>

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

谢谢你:)

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    给你

    $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; ?>
    

    【讨论】:

    • 非常感谢你,易卜拉欣 :) 你解开了我的谜语!谢谢你的帮助:)
    【解决方案2】:
    <?php
    if ($i == 4) {
        $i = 1;
    } else {
        $i++;
    }
    ?>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-04
        • 2022-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-24
        相关资源
        最近更新 更多