【问题标题】:Wordpress: loop only include posts X day's oldWordpress:循环仅包含 X 天前的帖子
【发布时间】:2009-09-07 17:11:20
【问题描述】:

我只想在我的 wordpress 首页上显示最近 X 天的帖子。比方说,一周,所以 7 天。

告诉 wordpress 只选择循环中最后 X 天的帖子的正确方法是什么?

目前,我已经通过 hack 让它工作了,但它弄乱了分页。转到下一页不会选择正确的帖子。

//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

【问题讨论】:

    标签: php wordpress


    【解决方案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 天的帖子,page/1 在那天之后开始显示帖子,page/2 按预期作为 page/1 的延续。

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      相关资源
      最近更新 更多