我想按时间顺序列出所有 Jekyll 帖子,并且同一类别中的所有帖子都将具有与我给出的相同的类名(例如cate1
,,cate2
... cate3
)。我已经尝试了很多方法,仍然无法解决这个问题。你能帮我吗?
例如,以下代码可以按时间顺序列出所有帖子,但所有链接都将具有相同的cate1
类。
{% for post in site.posts %}
{% if site.categories.CATEGORY1 %}
<a class="cate1" href="{{ post.url }}">{{ post.title }}</a>
{% elsif site.categories.CATEGORY2 %}
<a class="cate2" href="{{ post.url }}">{{ post.title }}</a>
{% elsif site.categories.CATEGORY3 %}
<a class="cate3" href="{{ post.url }}">{{ post.title }}</a>
{% endif %}
{% endfor %}
我也试过:
{% for post in site.posts %}
{% for post in site.categories.CATEGORY1 %}
<a class="cate1" href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}
{% for post in site.categories.CATEGORY2 %}
<a class="cate2" href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}
{% for post in site.categories.CATEGORY3 %}
<a class="cate3" href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}
{% endfor %}
这将生成每个链接 13 次,这是我的帖子总数。链接顺序基于循环中类别的顺序,而不是按时间顺序。
我也试过:
{% for post in site.posts %}
{% for CATEGORY1 in site.categories %}
<a class="cate1" href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}
{% for CATEGORY2 in site.categories %}
<a class="cate2" href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}
{% for CATEGORY3 in site.categories %}
<a class="cate3" href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}
{% endfor %}
这将生成每个链接 25 次。我想这是因为我有 5 个类别和 5*5=25。每个链接将有不同的类名(例如a_CATEGORY1_post.cate1
, a_CATEGORY1_post.cate2
, a_CATEGORY1_post.cate3
)。但所有帖子都是按时间顺序排列的。