带有年、月、日、帖子和评论计数的 Wordpress 存档列表

Posted

技术标签:

【中文标题】带有年、月、日、帖子和评论计数的 Wordpress 存档列表【英文标题】:Wordpress archive list with year, month, days, posts and comment counts 【发布时间】:2013-05-04 20:56:21 【问题描述】:

我愿意实现这样的 Wordpress 存档列表:

2013

5 月 (2)

04 - 我喜欢 Wordpress(3 cmets) 01 - 我真的很喜欢 Wordpress(1 条评论)

2 月 (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>

你可以在这里看到它的样子: http://www.vie-nomade.com/archives/

我知道需要将所有内容按月分开,然后按年分开。谢谢。

【问题讨论】:

vie-nomade.com/archives 不工作 【参考方案1】:

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

我有一个类似的列表,但只显示每个月的帖子数量,总共有 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 -->

【讨论】:

谢谢你,这很好用!当您谈论查询时,我听到了您的声音。确实有很多帖子。我在想每年一页可能会有所帮助?当有一个查询时,比如说 4 页。查询会在 4 内“中断”还是无论如何它仍然是一个巨大的大查询? WPML 用户注意:我在查询中添加了suppress_filters=0,否则它会显示每种语言的所有帖子。 您始终可以依赖 ajax 处理大量数据。也许你可以显示最近一个月的帖子,其余的可以用ajax请求,看看this它不一定是插件,它可以很容易地在一个主题中实现。 您可以通过查询帖子一次,然后使用 PHP 函数使用相同的帖子数组将它们分成年和月来达到相同的效果。 使用页面缓存也有意义(例如 W3 Total Cache)

以上是关于带有年、月、日、帖子和评论计数的 Wordpress 存档列表的主要内容,如果未能解决你的问题,请参考以下文章

访问令牌现在的行为与 2018 年 7 月 - 2 日之前不同

Facebook 图形 API 用于获取带有评论数和点赞数的 Facebook 订阅源

strstr() 的参数计数错误

如何在不使用 fql 的情况下获取帖子 URL 的 Facebook 点赞、分享和评论计数?

带有年、月、日、小时和分钟的 date-fns 倒计时

sql 在每天结束时关闭不同的计数