这个Simple Yearly Archive Plugin 就是这样。
此代码也可以实现以下功能:
<?php
// get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = \'post\' AND post_status = \'publish\' GROUP BY year DESC" );
foreach ( $years as $year ) {
// get posts for each year
$posts_this_year = $wpdb->get_results( "SELECT post_title FROM wp_posts WHERE post_type = \'post\' AND post_status = \'publish\' AND YEAR(post_date) = \'" . $year->year . "\'" );
echo \'<h2>\' . $year->year . \'</h2><ul>\';
foreach ( $posts_this_year as $post ) {
echo \'<li>\' . $post->post_title . \'</li>\';
}
echo \'</ul>\';
}
?>
这可能有优化的空间,但我已经测试过了,它是有效的。