简体   繁体   中英

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. Obviously, site.categories.category_name is taken literally, looking for a category named “category_name”.

Based on the Jekyll documentation , indexing (ie, [category_name] ) is no longer the correct answer. Now (since at least Jekyll v2), given a category name FOO , the correct way to list all posts of that category is

{% 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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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