Wordpress -how to return just current month posts on archive.php

守給你的承諾、 提交于 2019-12-13 04:49:36

问题


How to modify the wp_query to take in count post by month as per URL 2012/10/ ?

The following will list ALL post, where I just want to list post for 2012/10/

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(  'posts_per_page' => 10, 'paged' => $paged, 'orderby' => 'date', 'order' => 'DESC' );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); 

Any suggestions much appreciated.


回答1:


There's a code snippet provided in the link from the earlier answer that says "Returns posts for just the current week". Change "week" for "month", and you got your answer.

The code snippet would look like this:

$year = date('Y');
$month = date('n');
$query = new WP_Query( 'year=' . $year . '&monthnum=' . $month );

Just read the link. It's all there.

EDIT: If there are further problems returning the month, it may be because the variable needs a leading zero. In that case, the $month variable should be

$month = date('m');



回答2:


You can do that with the year and monthnum parameters, as described in the section Time Parameters in the class reference:

// ... set up your argument array for WP_Query:
$args = array( 
    'posts_per_page' => 10,
    'paged' => $paged,
    'orderby' => 'date',
    'order' => 'DESC' ,
    // add the year/month query here:
    'year' => 2012,
    'monthnum' => 10

    );

$wp_query = new WP_Query($args);

// ... and so on


来源:https://stackoverflow.com/questions/12898297/wordpress-how-to-return-just-current-month-posts-on-archive-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!