簡體   English   中英

在計數循環中使用if語句選擇特定帖子

[英]Selecting specific posts with an if statement in a counted loop

我目前正在統計循環中的帖子...

<?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()

我建議您檢查模板是否存在,如果不存在,請使用始終存在的主題默認值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM