Wordpress存档列表,包括年、月、日、帖子和评论计数

Wordpress存档列表,包括年、月、日、帖子和评论计数,wordpress,archive,Wordpress,Archive,我愿意实现如下Wordpress归档列表: <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

我愿意实现如下Wordpress归档列表:

<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 -->
2013 五月二日 04-我喜欢Wordpress(3条评论)
01-我真的很喜欢Wordpress(1条评论)

二月(一) 02-我喜欢Wordpress吗

2012

根据我在别处读到的内容,我必须创建自己的查询。我不是一个真正的开发者。我从这里开始:

<ul>
<?php
$args=array(
    'post_type' => 'post',
    'posts_per_page' => '500', /*no limit, how?*/
    'orderby' => 'date',
    'order' => 'DESC',
    );
query_posts($args);
while (have_posts()) : the_post();
?>

<li><?php the_time('j'); ?> | <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>  <?php comments_number( '', '(1)', '(%)' ); ?></li>

<?php endwhile; ?>
</ul>
  • |
您可以在此处看到它的外观:


我知道我们需要把所有的东西按月分开,然后按年分开。谢谢。

您可能想考虑与查询所有发布的帖子相关的性能问题。

我得到了一个类似的列表,但只显示每个月的帖子数量,总共有70个查询到db,如果我将其更改为显示我在该博客中得到的每个帖子,那么查询数量将增加到531个。(当然包括网站上的其他功能)

每月清单:

每个帖子列表:

如果您决定使用每月列表,您可以使用

[/警告结束]

如果你没有写那么多,只得到了几篇帖子,你应该找这样的东西:

<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 -->
      • ()

谢谢你,恰克,这台机器工作得很好!当你谈论问题时,我听到了你的声音。确实有很多帖子。我在想每年一页可能会有帮助?当有一个4页的查询时。查询是否会在4年内被“破坏”,或者无论如何它仍然是一个巨大的查询?WPML用户注意:我在查询中添加了
suppress\u filters=0
,否则它会以每种语言显示所有帖子。对于大量数据,您可以始终依赖ajax。也许你可以显示最近一个月的帖子,其余的可以用ajax请求,看看它不一定是一个插件,它可以很容易地在一个主题中实现。你可以通过查询一次帖子来达到同样的效果,并使用PHP函数使用相同的POST数组将它们拆分为数年和数月。使用页面缓存(例如W3 Total cache)不工作也是有意义的