我想按時間順序列出所有Jekyll帖子,並且同一類別中的所有帖子將與我給出的課程名稱相同(例如cate1
,cate2
,cate3
...) 。我嘗試了很多方法,但仍然無法弄清楚。你能幫我嗎?在列出所有Jekyll帖子時自動添加課程
例如,以下代碼可按時間順序列出所有帖子,但所有鏈接將具有相同的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
)。但所有帖子都是按照時間順序排列的。
非常感謝您的解釋,併爲我可憐的英語感到抱歉。我在帖子循環中添加了一個for循環,但Jekyll會多次生成每個鏈接。 – Peiwen
嗨@Peiwen使用#2以上。 –
#2作品完美!非常感謝你。只是好奇,如果它很容易使用類名稱,如cate1,cate2,cate3而不是類別名稱?這將更符合我的jQuery代碼。如果不是,我會在jquery中找到一些解決方法。感謝 – Peiwen