我目前正在计算循环中的帖子...
<?php $count = 0; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $count++; ?>
<?php include(locate_template('content.php')); ?>
<?php endwhile; ?>
但需要在if
语句中以编程方式选择特定帖子。
我需要选择的帖子数依次是 1、4、5、8、9、12、13 等(+3+1r)。
如何选择这些帖子(无需手动输入数字)?
虽然从技术上讲,WordPress偏离了主题,但我认为这是很多用户会询问的事情,特别是那些对WordPress和PHP都是新手的。
正如您的问题的评论中所建议的那样,您可以使用模运算符来检查这一点,希望这个答案可以解决您的问题。
<?php
$count = 0;
/* Start the Loop */
while ( have_posts() ) : the_post();
$count++;
if($count % 4 === 0 || $count % 4 === 1) :
locate_template('content.php', true);
endif;
endwhile;
?>
作为旁注,locate_template
函数将自动加载带有require_once的模板文件(如果您设置了 $load
参数),因此您无需将其包装在 include()
中。
我建议您检查模板是否存在,如果没有,则回退到将始终存在的主题默认值。