我有一個WordPress主題,我有一個情況,在主循環模板(index.php)我需要一些時間運行自定義循環。所以它看起來像這樣:有沒有辦法在不重複代碼的情況下運行這兩個循環?
$the_qry = new WP_Query(array('post_type' => 'post', 'offset' => 4, 'orderby' => 'date', 'order' => 'DESC', 'post_status' => 'publish', 'paged' => $paged));
if ($the_qry->have_posts()) : while ($the_qry->have_posts()) : $the_qry->the_post();
,但我需要它運行正常
if (have_posts()) : while (have_posts()) : the_post();
我試着包裝他們在IF這樣的其他情況:
if ($special_situation == true) {
if ($the_qry->have_posts()) : while ($the_qry->have_posts()) : $the_qry->the_post();
} else {
if (have_posts()) : while (have_posts()) : the_post();
}
// output each post code
endwhile; endif;
但它當然不起作用,因爲IF沒有在IF語句中關閉,所以它不會以這種方式工作。我能想到的唯一的解決辦法是要做到這一點:
if ($special_situation == true) {
if ($the_qry->have_posts()) : while ($the_qry->have_posts()) : $the_qry->the_post();
// output each post code
endwhile; endif;
} else {
if (have_posts()) : while (have_posts()) : the_post();
// output each post code
endwhile; endif;
}
但似乎愚蠢的,我因爲// output each post code
不會改變,所以我重複了很多代碼。
有沒有辦法簡化這一切?
謝謝!
爲什麼不把'每個郵政編碼'輸出到函數中並在每個地方調用該函數? –
我也考慮過,但我希望我可以將它全部保留在一個文件中,並簡單地操作if/while/the_post()部分。 – user3245789