目前我博客上的档案显示如下:
January, 2016(1)
February, 2016(2)
February, 2015(3)
January, 2014(4)
我想要的是按年份排序,只列出当年的月份。它应该显示如下:
January, 2016(1)
February, 2016(2)
2015
2014
等等。
MySQL查询是:
function get_dates_by_post_type( $post_type, $limit = 10 )
{
global $wpdb;
$datestr = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts
FROM $wpdb->posts
WHERE post_type = '$post_type'
AND post_status = 'publish'
GROUP BY YEAR(post_date)/*, MONTH(post_date)*/
ORDER BY post_date DESC
LIMIT $limit";
$dates = $wpdb->get_results($datestr, OBJECT);
return $dates;
}
用于显示档案的代码:
<ul class="sub-menu-list-lvl-3">
<?php
$dates = get_dates_by_post_type($post_type);
foreach ($dates as $archive) {
$month = mktime(0, 0, 0, $archive->month, 1, 2005);
$the_date = date('F', $month).' '.$archive->year;
if(is_arabic())
{
global $Ar;
$time = strtotime($the_date);
$fix = $Ar->dateCorrection($time);
$Ar->setMode(2);
$the_date = find_and_replace_arabic_numbers($Ar->date('M Y', $time, $fix));
}
if ( isset( $query_vars['year'] ) && $query_vars['year'] == $archive->year && isset( $query_vars['monthnum'] ) && $query_vars['monthnum'] == $archive->month )
{
$active = 'active';
}
else
{
$active = '';
}
echo '<li>'.
'<a class="sub-menu-lvl-3 slidingDoors '.$active.'" href="'.get_month_link( $archive->year, $archive->month ).'?pt='.$post_type.'" title="'.$the_date.'">'.
'<img src="'.get_bloginfo( 'template_url' ).'/assets/img/lvl-3-sub-nav-t.png" class="btnT" alt="">'.
'<span class="inlineBlock">'.$the_date.' ('.find_and_replace_arabic_numbers($archive->posts).')</span>'.
'<img src="'.get_bloginfo( 'template_url' ).'/assets/img/'.$subfolder.'lvl-3-sub-nav-r.png" class="lvl-3-arrow" alt="">'.
'<img src="'.get_bloginfo( 'template_url' ).'/assets/img/lvl-3-sub-nav-b.png" class="btnB" alt="">'.
'</a>'.
'</li>';
}
?>
</ul>