我已经设法在1月份浏览了本月的帖子,但我正在努力在去年12月获得上个月的帖子。我想这就是问题所在,那就是2014年,而我所追求的职位是2013年(12月)。如果有人能发现问题,我将不胜感激。我浏览了大量的例子,抄本和WordPress论坛,但没有找到任何有用的东西。
<!-- PREVIOUS MONTH NEWS -->
<?php
$prevMonth = date(\'M\', mktime(0, 0, 0, date(\'m\'), 0, date(\'Y\')));
$newsPrevArgs = array(
\'cat\' => \'news\',
\'monthnum\' => $prevMonth,
\'order\' => \'DES\',
\'posts_per_page\' => 5
);
?>
<div class="clearfix"></div>
<h3 class="news-previous-month"><?php echo date(\'M Y\', mktime(0, 0, 0, date(\'m\'), 0, date(\'Y\'))) ?></h3>
<?php
$previous_news = new WP_Query( $newsPrevArgs );
$isFirstNewsLoop = true;
$newsItemNum = 0;
// Loop through all news items
while ( $previous_news->have_posts() ) {
$previous_news->the_post(); ?>
<?php
echo \'<div class="news-article news-article-\' . $newsItemNum . \' \' . ( $isFirstNewsLoop ? \'news-article-current\' : \'\') . \'">\';
echo \'<span class="news-date">\' . get_the_date(\'d M Y\', \'<strong>\', \'</strong>\') . \'</span>\';
echo \'<span class="news-thumb">\' . get_the_post_thumbnail($page->ID, \'home-widget-210\') . \'</span>\';
echo \'<span class="news-title"><strong>\' . get_the_title() . \'</strong></span>\';
echo \'<span class="news-blurb">\' . get_the_excerpt() . \'</span>\';
wp_reset_postdata();
echo \'</div>\'
?>
<?php
$newsItemNum +=1;
$isFirstNewsLoop = false;
?>
<?php } ?>
最合适的回答,由SO网友:socki03 整理而成
这可能是一个简单的PHP问题,即您如何要求返回第一个日期()
date(\'M\', mktime(0, 0, 0, date(\'m\'), 0, date(\'Y\')));
正在返回Dec,WP\\u Query需要月份号。首先,尝试将“M”改为“M”。
date(\'m\', mktime(0, 0, 0, date(\'m\'), 0, date(\'Y\')));
如果不起作用,请尝试使用
date_query 也把这一年算进去。
祝你好运
SO网友:Shazzad
您需要将year与monthnum结合使用。
// Calculate month or year based on your WordPress timezone setting
// As post date is based on the timezone you set on general setting page on wp-admin
$current_month_num = date(\'m\', current_time(\'timestamp\') );
$current_month_year = date(\'Y\', current_time(\'timestamp\') );
// if current month number is less that 2, what\'s probably 1|January
// then the last month will be 12|December
// and year will be last year
if( $current_month_num < 2 )
{
$prev_month_num = 12;
$prev_month_year = $current_month_year - 1;
}
// if month number is not less that 2, then previous month will be from current year
else
{
$prev_month_num = $current_month_num - 1;
$prev_month_year = $current_month_year;
}
// The query argument
$newsPrevArgs = array(
\'cat\' => \'news\',
\'year\' => $prev_month_year,
\'monthnum\' => $prev_month_num,
\'order\' => \'DES\',
\'posts_per_page\' => 5
);