您可以查询当前查看年份之前发表的单个帖子。按日期降序(默认)排序时,第一个结果将来自最近一年的帖子。
$current_year = get_query_var( \'year\' );
$previous_posts = get_posts( array(
\'numberposts\' => 1,
\'date_query\' => array(
\'year\' => $current_year,
\'compare\' => \'<\',
),
) );
if ( ! empty( $previous_posts ) ) {
$previous_year = get_the_time( \'Y\', $previous_posts[0] );
}
您也可以通过帖子获得下一年,但您需要交换顺序,否则您将获得最新帖子的年份:
$next_posts = get_posts( array(
\'numberposts\' => 1,
\'order\' => \'ASC\',
\'date_query\' => array(
\'year\' => $current_year,
\'compare\' => \'>\',
),
) );
if ( ! empty( $next_posts ) ) {
$next_year = get_the_time( \'Y\', $next_posts[0] );
}