簡體   English   中英

在WordPress中將帖子分類為一個月?

[英]categories the posts month wise in wordpress?

我試圖在wordpress中檢索最近8個月的帖子。 我已經使用以下代碼來做到這一點

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

現在我想將每個月的帖子放置在單獨的數組中,以便我可以在前端按月顯示它們。 任何想法嗎?

您可以使用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
    }

我認為這對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM