2009-09-07 97 views
0

我想只顯示我的wordpress首頁上最後X天的帖子。比方說,一週,所以7天。WordPress的:循環只包括X天的舊帖子

有什麼方法告訴wordpress只選擇循環中最後X天的帖子?

目前,我已經通過黑客工作,但它弄亂了分頁。轉到下一頁不會選擇正確的帖子。

//Hack found on the bottom of http://codex.wordpress.org/Template_Tags/query_posts 
function filter_where($where = '') { 
    //posts in the last 7 days 
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'"; 
} 
add_filter('posts_where', 'filter_where'); 
query_posts($query_string); 
if (have_posts()) : while (have_posts()) : the_post(); 
... and the usual 

回答

1

您可以用下面的部分成功:

function filter_where($where = '') { 
    $date_split = date('Y-m-d', strtotime('-7 days')); 
    if (is_paged()) { 
    $where .= " AND post_date < '" . $date_split . "'"; 
    } else { 
    $where .= " AND post_date > '" . $date_split . "'"; 
    } 
    return $where; 
} 
add_filter('posts_where', 'filter_where'); 
query_posts($query_string); 

我的主頁顯示在過去7天的帖子,頁面/ 1開始顯示,一天後的職位和頁/ 2作爲一個延續如預期的那樣。

相關問題