简体   繁体   English

如何在Jekyll中列出同一类别的帖子?

[英]How do I list posts from the same category in Jekyll?

I'd like to list a fixed number of recent posts having the same category as the current post. 我想列出与当前帖子具有相同类别的固定数量的最近帖子。 This is what I have arrived at: 这就是我所得到的:

{% for category_name in page.categories limit:1 %}
    <h2>Other articles in {{ category_name }}</h2>
    <ul>
        <!-- now what? -->
    </ul>
{% endfor %}

I know about site.categories , but I don't know how to subscript the dictionary. 我知道site.categories ,但我不知道如何下标字典。 Obviously, site.categories.category_name is taken literally, looking for a category named “category_name”. 显然, site.categories.category_name字面意思,寻找名为“category_name”的类别。

Based on the Jekyll documentation , indexing (ie, [category_name] ) is no longer the correct answer. 根据Jekyll文档 ,索引(即[category_name] )不再是正确的答案。 Now (since at least Jekyll v2), given a category name FOO , the correct way to list all posts of that category is 现在(至少是Jekyll v2),给定一个类别名称FOO ,列出该类别所有帖子的正确方法是

{% for post in site.categories.FOO %}
    <li>{{ post.title }}</li>
{% endfor %}

To note, I recently ran into this issue, my configuration is 要注意,我最近遇到了这个问题,我的配置是

$ jekyll -v
jekyll 2.0.3
{% for post in site.categories[category_name] %}
    <li>{{ post.title }}</li>
{% endfor %}

This works for me: 这对我有用:

{% for post in site.categories.FOO %}
 + [{{ post.title }}]({{ page.url }})
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM