2014-09-24 66 views
0

我正在嘗試檢索wordpress中最近8個月的帖子。我用下面的代碼來做到這一點在wordpress中分類明智的帖子?

$args = array(
       'posts_per_page' => -1, 
       'date_query' => array(
       array(
       'column' => 'post_date_gmt', 
       'after' => '5 month ago', 
       ) 
       ) 
    ); 
$query = new WP_Query($args); 

現在我想把它放到單獨的陣列每個月的帖子,這樣我可以給他們一個月明智的前端。 有關這個的任何想法?

+0

你迭代結果andat每一步把結果相應的數組中 – 2014-09-24 06:33:58

回答

0

您可以使用一個for循環:

$date = date('Y-m-d'); 
    $dateArray = array(); 
    $dateArray[] = $date; 
    for($i=1; $i<8 ; $i++){ 
     $month = '- ' . $i . 'month'; 
     $old_date = strtotime ($month , strtotime ($date)) ; 
     $old_date = date('Y-m-d', $old_date); 
     $dateArray[] = $old_date; 
    } 
    foreach($dateArray as $item){ 
     $month_loop = date("m", strtotime($item)); 
     $year_loop = date("Y", strtotime($item)); 
     $query = new WP_Query('year=' . $year_loop . '&monthnum=' . $month_loop); 
     // Do action 
    } 

我認爲這有助於

相關問題