如何在jekyll上显示我主页上的最新帖子?

kab*_*bir 48 templates templating liquid-layout liquid jekyll

<ul class="entries">
  {% for post in paginator.posts %}
  <li>
    <a href="{{ post.url }}">
    <h3>{{ post.title }}</h3>
    <p class="blogdate">{{ post.date | date: "%d %B %Y" }}</p>
    <div>{{ post.content |truncatehtml | truncatewords: 60 }}</div>
    </a>
  </li>
  {% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)

这显示了我的所有帖子,我只想展示最新的帖子.

Dan*_*ird 106

这可以通过以下方式实现limit:

{% for post in site.posts limit:1 %}
... Show the post ...
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

您还可以使用limitoffset一起"发布"最近的帖子:

<h1>Latest Post</h1>
{% for post in site.posts limit:1 %}
... Show the first post all big ...
{% endfor %}
<h1>Recent Posts</h1>
{% for post in site.posts offset:1 limit:2 %}
... Show the next two posts ...
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

  • 旁注,出于可访问性原因,您每页只需要一个`&lt;h1&gt;`标签。很棒的回答,谢谢!很有帮助。 (2认同)

Mer*_*vex 16

而不是创建一个循环,只需分配变量并继续...

{% assign post = site.posts.first %}
Run Code Online (Sandbox Code Playgroud)

(编辑2018)由于有人想知道如何在完成此操作后迭代其他帖子:

{% for post in site.posts offset:1 %}
  ... Show the next posts ...
{% endfor %}
Run Code Online (Sandbox Code Playgroud)


Dou*_*ell 5

如果您是按照标题“我该如何在我的主页上使用jekyll显示最新帖子”来回答问题,?而不是“如何仅显示模板中的最新帖子”,以下内容可能会有所帮助。

给定具有默认最小值主题的全新Jekyll版本3.7.3,请创建_layouts/home.html包含以下内容的文件:

---
layout: none
---
{{ site.posts.first }}
Run Code Online (Sandbox Code Playgroud)

使Jekyll 3.7.3使用帖子模板将第一个帖子显示为主页。