可以achievethis不循环,使用许多黑客之一WP。 首先创建一个名为filter_where()的函数,它包含一个SQL“WHERE”条件。然后,在启动循环之前,将filter_where()函数挂接到WordPress的post_where()函数中。
因此,filter_where()函数中包含的“WHERE”子句将添加到post_where()函数中包含的SQL查询的末尾,这意味着该循环将返回仅在两个在filter_where()函数中指定的日期。
,用今天的日期和上周的日期,自动生成 EXAMLPE:
<?php
$date_current = date("Y-m-d");// current date
$date_old = strtotime(date("Y-m-d", strtotime($date)) . " -1 week");
function filter_where($where = '') {
$where .= " AND post_date >= $date_old AND post_date <= $date_current ";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
while (have_posts()) :
the_post();
the_content();
endwhile;
?>
我尝试使用WP_Query,但它不是取结果 – 2014-09-05 14:07:23
针对日期参数你说你已经尝试过WP_Query,或者我的代码不工作? – Mastrianni 2014-09-05 14:11:00
@mastianni不,我说我已经尝试WP_Query,但是看到你的代码后,我发现我的错误:)感谢很多 – 2014-09-05 14:23:18