我怎么能只显示我与哲基尔主页最近的帖子?

问题描述 投票:45回答:4
<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>

这显示了我的所有文章,我只是想显示最近。

templates jekyll templating liquid liquid-layout
4个回答
105
投票

这可以通过使用limit来完成:

{% for post in site.posts limit:1 %}
... Show the post ...
{% endfor %}

您还可以使用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 %}

16
投票

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

{% assign post = site.posts.first %}

(编辑2018)由于有人想知道如何遍历其他职位你这样做后:

{% for post in site.posts offset:1 %}
  ... Show the next posts ...
{% endfor %}

4
投票

如果你来到这里的问题,在标题中说:“我怎么能只显示我与哲基尔主页最近的帖子?”而不是“我怎么只显示模板中的最新帖子,”下面可能会有所帮助。

给定一个全新的哲基尔版本3.7.3安装使用默认极小的主题,创建一个文件,_layouts/home.html包含以下内容:

---
layout: none
---
{{ site.posts.first }}

原因杰基尔3.7.3显示后的第一次,使用后的模板,为主页。


0
投票

看样子你也可以只通过site.posts的第一个指数作为访问最新帖子:

{%- assign latest_post = site.posts[0] -%}

Latest post: <a href="{{ latest_post.url }}">{{ latest_post.title }}</a>

虽然site.posts.first工程太为别人所提到的,上面的例子还提供了一个一致的方式访问其他指标除了刚刚第一个(不是说你会永远需要)。另外,我没有足够的声誉,这个答案添加作为注释,而不是:)

© www.soinside.com 2019 - 2024. All rights reserved.