我认为这是正确的:
$year = 2013;
$month = 0;
$qry = new WP_Query(
array(
\'post_type\'=>\'post\',
\'posts_per_page\'=>-1,
\'orderby\'=>\'date\',
\'order\'=>\'ASC\',
\'ignore_sticky_posts\' => true,
\'year\' => $year,
)
);
while ($month < 12) {
$month++;
echo date(\'M\',strtotime(\'2000-\'.str_pad($month, 2, \'0\', STR_PAD_LEFT).\'-01 00:00:01\')).\'<br>\';
if ($qry->have_posts()) {
while ($qry->have_posts()) {
if (date(\'n\',strtotime($qry->post->post_date)) == $month) {
echo " -- ";
the_title();
echo \'<br />\';
$qry->the_post();
} else {
break;
}
}
}
}
当然,您需要设置
$year
到你真正想要的那一年。
这应该贯穿整个12个月,echo
检查每一个,检查中匹配的帖子$qry
后果
只打印月份名称和有帖子的月份的链接。
$year = 2013;
$month = 0;
$qry = new WP_Query(
array(
\'post_type\'=>\'post\',
\'posts_per_page\'=>-1,
\'orderby\'=>\'date\',
\'order\'=>\'ASC\',
\'ignore_sticky_posts\' => true,
\'year\' => $year,
)
);
$months = array();
if (!empty($qry->posts)) {
foreach ($qry->posts as $p) {
$months[date(\'n\',strtotime($p->post_date))] = $p->post_date;
}
}
while ($month < 12) {
$month++;
$monthname = date(\'M\',mktime(0,0,0,$month,1,$year));
echo \'<li>\';
if (isset($months[$month])) {
echo \'<a class="year" href="\'.get_month_link($year,$month).\'" />\'.$monthname.\'</a>\'.\'<br>\';
} else {
echo $monthname.\'<br>\';
}
echo \'</li>\';
}