尽管这不是最传统的做法,但我还是会这样做:
Edit : precision我创建了一个新的文件类别。php,它是类别的模板,并将这段代码放在其中。
$args = array( \'post_type\'=>\'post\',
\'posts_per_page\'=> -1,
\'post_status\'=>\'publish\',
\'orderby\'=>\'post_date\',
\'order\'=>\'DESC\'
);
$query = new WP_Query($args);
if($query->have_posts())
{
$years = array();
$postList = \'\';
foreach ($query->posts as $post) {
//if we haven\'t proceed the year yet
// we display it
if(!in_array($year = get_the_date(\'Y\',$post->ID),$years))
{
$postList.=sprintf(\'<h2>%s</h2>\',$year);
$years[] = $year;
}
$postList.=sprintf(\'<h3><a href="%1$s" title="%2$s">%2$s</a></h3>\',get_permalink($post->ID),$post->post_title);
}
echo $postList;
}else{
_e(\'No posts sorry\',\'your-text-domain\');
}