可以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