我们为学校安装了多个WP。他们需要逐年显示列表排序,以及当年使用的类别。例如:
2014年的问题
2013年的问题
等
他们可以告诉我在模板或插件中实现这个的代码吗?
答案 0 :(得分:0)
虽然这会输出类别(每个帖子都有它的类别),但与上面提到的格式略有不同,但这会让你开始:
<?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" ); //assuming that you have default wordpress table prefix
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 . "'" ); //assuming that you have default wordpress table prefix
echo '<p>Issues of ' . $year->year . '</p><ul>';
foreach ( $posts_this_year as $post ) {
$category = get_the_category( $post );
$post_url = get_permalink( $post->ID);
echo '<li>' $category[0]->cat_name.' | '. '<a href="'.$post_url.'">'.$post->post_title . '</a></li>';
}
echo '</ul>';
}
?>