【问题标题】:Trying to print a div every 4th loop in Wordpress尝试在 Wordpress 中每 4 个循环打印一个 div
【发布时间】:2015-08-31 09:57:05
【问题描述】:

我在这里有一个循环,它正在发布我的自定义帖子类型的完整列表 - 但是,每第四个循环我都会尝试打印一个 clearfix,这就是我想出的,但它要么在每个循环中打印它,要么根本没有——有人可以帮忙吗?干杯

<?php
    $args = array(
    'post_type' => 'custom',
    'numberposts' => '-1',
    'post_status' => 'publish',
    'order' => 'ASC'
    );

    $postslist = get_posts($args);
    foreach ($postslist as $post) :
    setup_postdata($post);

    for ($counter = 1; $counter < 100; $counter++ ) {
        if ($counter % 4 == 0) {
            echo "<div class='clearfix'></div>";
     }  
   }
?>

【问题讨论】:

    标签: wordpress loops counter


    【解决方案1】:

    不需要for循环。你可以试试这个我希望它会工作。

    <?php
            $args = array(
            'post_type' => 'custom',
            'numberposts' => '-1',
            'post_status' => 'publish',
            'order' => 'ASC'
            );
    
            $postslist = get_posts($args);
            $count = 1 ;
            foreach ($postslist as $post) : 
            setup_postdata($post);
            if($count % 4 ==0){
              echo '<div class="clear"></div>';
             }
            $count++;
         endforeach; ?>
    

    【讨论】:

    • 不幸的是,这不会打印 div
    • 对不起,我忘记了 $count++。你能再试一次吗
    猜你喜欢
    • 2019-05-15
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多