[英]Loop through the first 3 items of an array in Liquid
我正在創建一個網站,讓您可以根據從人們調查的信息中瀏覽人們想要的禮物(我只會調查一小部分人,除非該網站引起了一些關注 - 我不擔心在網站,但這不是我在這里要問的問題)。
我正在使用 Jekyll 對網站進行編碼,所以我當然需要使用 Liquid。
你看,我想對site.posts
數組中的前 3 個項目進行排序。
我查看了數組過濾器文檔,但似乎找不到任何可以完成我需要完成的工作。
我讀了這篇文章,但我不完全確定如何理解那里的答案,因為我不知道“第三個循環”是什么,而且我循環瀏覽帖子的方式不能很好地訪問使用site.posts[0]
、 site.posts[1]
來site.posts[0]
數組數據。 這會使代碼變得龐大,但我想如果需要的話我可以這樣做。
這是我現在擁有的代碼:
<h2>Recently added gifts</h2>
<div class="posts">
{% assign posts = site.posts %}
{% for post in posts %}
{% unless post.list %}
<div class="post">
<h3 class="post-title">
<a href="{{ post.url | relative_url }}">
{{ post.title }}
</a>
</h3>
<time datetime="{{ post.date | date_to_xmlschema }}" class="post-meta">{{ post.date | date_to_string }}</time>
<p class="post-excerpt">
{% if post.description %}
{{ post.description | strip_html }}
{% else %}
{{ post.excerpt | strip_html }}
{% endif %}
</p>
</div>
{% endunless %}
{% endfor %}
</div>
正如評論中所解釋的,並且由於答案可能對其他人有所幫助,您可以在遍歷數組時將限制設置為參數,如下所示:
{% for post in posts limit:3 %}
Do something
{% endfor %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.