您可能需要考虑与查询所有已发布帖子相关的性能问题。
我有一个类似的列表,但只显示每个月的帖子数量,总共有 70 个对数据库的查询,如果我将其更改为显示我在该博客中获得的每一篇文章,这个数字会上升到 531 个查询。(当然包括网站上的其他功能)
每月清单:
每个帖子列表:
如果您决定使用每月列表,则可以使用wp_get_archives。
[/警告结束]
如果不写那么多并且只有几篇文章,你应该寻找这样的东西:
<ul class="years">
<?php
$all_posts = get_posts(array(
'posts_per_page' => -1 // to show all posts
));
// this variable will contain all the posts in a associative array
// with three levels, for every year, month and posts.
$ordered_posts = array();
foreach ($all_posts as $single) {
$year = mysql2date('Y', $single->post_date);
$month = mysql2date('F', $single->post_date);
// specifies the position of the current post
$ordered_posts[$year][$month][] = $single;
}
// iterates the years
foreach ($ordered_posts as $year => $months) { ?>
<li>
<h3><?php echo $year ?></h3>
<ul class="months">
<?php foreach ($months as $month => $posts ) { // iterates the moths ?>
<li>
<h3><?php printf("%s (%d)", $month, count($months[$month])) ?></h3>
<ul class="posts">
<?php foreach ($posts as $single ) { // iterates the posts ?>
<li>
<?php echo mysql2date('j', $single->post_date) ?> <a href="<?php echo get_permalink($single->ID); ?>"><?php echo get_the_title($single->ID); ?></a> (<?php echo $single->comment_count ?>)</li>
</li>
<?php } // ends foreach $posts ?>
</ul> <!-- ul.posts -->
</li>
<?php } // ends foreach for $months ?>
</ul> <!-- ul.months -->
</li> <?php
} // ends foreach for $ordered_posts
?>
</ul><!-- ul.years -->