2009-09-07 37 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作为一个延续如预期的那样。

相关问题