创建类别查询,并按日期排序
$q = new WP_Query(\'category_name=acategory&orderby=date&order=DESC\');
之后,在循环帖子时,将年份保存在helper变量中,只有在发生更改时才显示:
$year = 0;
if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
$post_year = (int) get_the_time(\'Y\');
if ( $post_year !== (int) $year ) {
echo \'<h2>\' . $post_year . \'</h2>\';
$year = $post_year;
}
// the loop
echo \'<p><a href="\' . get_permalink() . \'">\' . get_the_title() . \'</a></p>\';
endwhile;
endif;
wp_reset_postdata();
unset($year, $post_year);