在计数循环中使用 if 语句选择特定帖子

Selecting specific posts with an if statement in a counted loop

我目前正在计算循环中的 posts...

<?php $count = 0; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
    <?php $count++; ?>
    <?php include(locate_template('content.php')); ?>
<?php endwhile; ?>

但需要在 if 语句中以编程方式 select 特定 post。

我需要 select 的 post 计数依次为 1、4、5、8、9、12、13 等 (+3+1r)。

如何 select 这些 post(无需手动输入数字)?

虽然在技术上与 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().

我建议您检查模板是否存在,如果不存在,则返回到始终存在的主题默认值。