要按年份描述和月份ASC显示列表,请执行以下操作:
使用custom\\u post更改您的帖子类型。
global $wpdb;
$posts = $wpdb->posts;
$sql = "SELECT DISTINCT(YEAR(`post_date`)) as years FROM $posts WHERE post_type = \'custom_post\' ORDER BY years DESC"; //Get all post year list by DESC
$result = $wpdb->get_results($sql);
foreach($result as $rs) {
echo \'<h2>\'.$rs->years.\'</h2>\';
$args = array(
\'post_type\' => \'custom_post\',
\'post_per_page\'=> -1,
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'ASC\',
\'date_query\' => array(array(
\'year\'=> $rs->years,
),),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
while($loop->have_posts()) : $loop->the_post();
echo \'<a href="\'.get_permalink().\'">\'.get_the_date().\'</a>\';
endwhile;
}
}