Detect each 4 using modulus php

前端 未结 2 1754
慢半拍i
慢半拍i 2021-01-22 16:43

I am trying to detect each 4th post to insert extra code in my layout in wordpress using modulus method but I just cant get it.

Here is a short example of mine:

相关标签:
2条回答
  • 2021-01-22 17:22

    When it comes to things like this, I always increment one on the if statement before calling modulo like so:

    if(($count+1)%4 == 0)

    This way it's easy for me to make a mental note that the statement naturally reads "if the current count is the 4th one then do:"

    0 讨论(0)
  • 2021-01-22 17:30

    You need to start your counter at 1, as you are increasing it at the end of the loop:

    <?php $count=1;?>
    

    Either that, or you increase it at the start of the loop / before the check:

    <?php
            $count++; 
            if ($count % 4 == 0){     
                echo '<div class="clear"></div>';
            }            
    ?>
    
    0 讨论(0)
提交回复
热议问题