仅使用循环加载最新帖子

问题描述 投票:0回答:1

到目前为止,我可以按照以下步骤进行操作:

  {% for post in collections.posts %}
  <div class="post" style="{% if loop.index > 1 %} display: none;{% endif %}">
<a href="{{ post.url }}">{{ post.data.title }}</a>
  <p class="text-sm text-gray-600">
    {{ post.date | humanDate }}.
  </p>
    {{ post.templateContent | safe }}  
  </div>
  {% endfor %}

问题是网站仍然会加载其余帖子,只是它们被隐藏了。如何停止其余帖子的加载?

static-site nunjucks eleventy 11ty
1个回答
0
投票

如果需要显示的唯一元素,则只能选择列表的最后一个元素:

<div class="post">
   {{ posts[-1].templateContent | safe }}  
</div>
© www.soinside.com 2019 - 2024. All rights reserved.