Hexo 中的随机帖子

Random posts in Hexo

我想使用 Hexo 在我的主页上显示 5 个随机帖子,但似乎无法正常工作?!我把'date'改成了'random' 但没用。

代码:

<ul>
  <% site.posts.sort('date', -1).limit(5).each(function(post){ %>
    <li>
      <a href="<%- url_for(post.path) %>"><%= post.title || '(no title)' %></a>
    </li>
  <% }) %>
</ul>

shuffle or the alias random 有效:

<ul>
  <% site.posts.random().limit(5).each(function(post){ %>
    <li>
      <a href="<%- url_for(post.path) %>"><%= post.title || '(no title)' %></a>
    </li>
  <% }) %>
</ul>

工作原理:

Hexo 使用 Warehouse for its database. posts is a Query object. So to modify the posts in the future just find the right database Query method in the warehouse API。每个 Query 方法 returns 是前一个 Query 的修改副本,以便可以链接这些方法。因此,如果您想再次修改它,只需找到另一种方法并将其链接即可。希望这对您有所帮助!