2013-02-25 181 views
0

我目前有一個頁面只顯示1篇文章,因爲我想每天發佈1篇文章,並在尊敬中顯示。創建一個wordpress循環,只顯示當天的帖子

我決定在一天中有多個帖子,但我只希望主頁僅顯示當天的帖子。

什麼在我的循環中,我可以改變來完成這個?

我的網站是http://thatshitsawesome.com作爲參考。

+0

你可以發佈你的'的loop'的樣本? – hexblot 2013-02-25 21:09:11

+0

這裏是我的循環目前: '<?php if(have_posts()):?> <?php while(have_posts()):the_post(); ?!> \t \t

> \t
\t \t \t

THIS AWESOME SHIT IS CATEGORIZED AS

\t
\t
< - 結束後 - > ' – user2108869 2013-02-25 21:57:35

+0

你需要改變這一行'<?php while(have_posts()):the_post(); ?>' – hexblot 2013-02-25 22:01:21

回答

1

首先,您必須增加最多顯示的可見帖子數量。我假設你已經知道如何做到這一點,因爲你已經設法將其限制爲每個查詢一個。爲了完成,您可以使用查詢參數中的posts_per_page條件或在管理面板中設置下設置的「博客頁面最多顯示」值來更改它,如果您要使用默認值。

要將帖子限制到當天,請使用WP_Query參考中定義的某些特定時間參數。您需要條件year,monthnumday

例子:

<?php 
// Limit query posts to the current day 
$args = array(
    'year' => (int) date('Y'),  
    'monthnum' => (int) date('n'),  
    'day' => (int) date('j'), 
); 

$query = new WP_Query($args); 

// The Loop 
while ($query->have_posts()) : 
    $query->the_post(); 

    // ... 
endwhile; 
?> 

如果你不使用一個明確的查詢,但依靠內部WP查詢的常用方法改變內部查詢使用pre_get_posts行動。將以下功能添加到您的functions.php文件中,以僅顯示當天的帖子,並僅顯示在FrontPage中。

例子:

<?php 
function limit_posts_to_current_day($query) { 
    if ($query->is_home() && $query->is_main_query()) { 
     $query->set('year', (int) date('Y')); 
     $query->set('monthnum', (int) date('n')); 
     $query->set('day', (int) date('j')); 
    } 
} 
add_action('pre_get_posts', 'limit_posts_to_current_day'); 
?> 
相關問題