我们可以计算WordPress循环并将其存储在变量中吗?
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $post_id = get_the_ID(); ?>
<?php get_template_part(\'content\',\'home\'); ?>
some code to be executed
<?php endwhile; ?>
<?php endif; ?>
我想要的是,对于每5篇文章,应该执行提取的ceratin代码(示例新闻稿)→ 这意味着在循环中,每隔5h、10th、15th、\\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu无限个帖子(5n,n>=1)应该执行代码。
最合适的回答,由SO网友:Michael 整理而成
$wp_query->current_post
是内置循环计数器,循环中的第一个post从0零开始。
所以这条线some code to be executed
可以转换为:
<?php if( $wp_query->current_post > 0 && $wp_query->current_post % 5 == 0 ) { ?>
some code to be executed
<?php } ?>