您可以创建一个助手函数,该函数每年仅返回一次年份号:
function get_unique_year( $post_id = 0 )
{
static $last = 0;
$post_id || $post_id = get_the_ID();
$year = get_the_time( \'Y\', $post_id );
if ( $year === $last )
return;
$last = $year;
return $last;
}
然后获取您的帖子并使用该助手:
$posts = wp_get_recent_posts(); // or any other function returning posts
foreach ( $posts as $post )
if ( $year = get_unique_year( $post->ID ) )
print "Year: $year<br>";