PHP-以循环中的第5个和第9个元素为目标


PHP - Targeting every 5th and 9th element in the loop

我有一个无限循环,它为WordPress帖子页面循环。我已经找到了一个简单的循环,它计算循环中的元素,并用Featured Posts替换5th9th元素。我的想法是针对每一个第5和第9元素,但现在只针对前5和第九元素。

这是我的循环:

<div class="loop">
<?php /* Start the Loop */ ?>
<?php 
    $i=1;
    while ( have_posts() ) : the_post(); 
        if ($i==5) { ?>
            <article>
            </article><!-- featured-post-ends-here -->
            <?php get_template_part( 'content', $post->post_type ); ?>
        <?php } else if ($i==9) { ?>
            <article>
            </article><!-- featured-post-ends-here -->
            <?php get_template_part( 'content', $post->post_type ); ?>  
        <?php } else {
            get_template_part( 'content', $post->post_type ); 
        }
        if ($i==9) {
            $i=0;
        }
        $i++; 
    endwhile; ?>
</div><!-- .loop -->

我知道我快到了,但有点坚持了下来。

提前谢谢。

您可以使用模数%运算符。

if( (($i%9) == 0) OR (($i%5) == 0) ) {
   //Featured post
}

https://eval.in/203973