2013-04-14 39 views
0

我想按時間順序列出所有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)。但所有帖子都是按照時間順序排列的。

回答

2

你會想另一個循環職位循環是這樣的:

{% for post in site.posts %} 
    {% for cat in post.categories %} 
     <a class="{{ cat }}" href="{{ post.url }}">{{ post.title }}</a> 
    {% endfor %} 
{% endfor %} 

在這裏看到什麼是在循環訪問細節:
https://github.com/mojombo/jekyll/wiki/template-data

有兩個問題這種方法:

  1. 如果您的其中一條信息是不是有一個類別分配給它,它不會被列出。所以你可能也想做一個檢查。

  2. 如果分配給多個類別,您將獲得多次列出的相同帖子。所以,爲了這個,我會使用液體過濾器有重做代碼,它像:class="cat1 cat2 cat3"這樣的:

    {% for post in site.posts %} 
        <a class="{{ post.categories | join: ' ' }}" href="{{ post.url }}">{{ post.title }}</a> 
    {% endfor %} 
    

看到這裏的液體過濾器:
https://github.com/shopify/liquid/wiki/liquid-for-designers

編輯:你的意思你想添加一個獨特的類到每個鏈接,而不是類別名稱(S)?不知道你的意思是具體的。對於獨特你可以在<a href部分之前做case聲明:

{% case condition %} 
    {% when cat = 'cat1' %} 
     cat = 'unique_class_1' 
    {% when cat = 'cat2' %} 
     cat = 'unique_class_2' 
    {% else %} 
     ... else ... 
    {% endcase %} 

但在這種情況下,爲什麼不類類夠獨特之處?

編輯例3 Peiwin的評論 - 我不建議做這種方式,因爲它是編碼不良做法。相反,與您的jQuery工作,使其更加靈活,並與#2以上工作:

{% for post in site.posts %} 
    {% for cat in post.categories %} 
    {% case condition %} 
     {% when cat = 'cat1' %} 
     {% assign cat_class = 'unique_class_1' %} 
     {% when cat = 'cat2' %} 
     {% assign cat_class = 'unique_class_2' %} 
     {% else %} 
     ... else ... 
    {% endcase %} 
    {% endfor %} 

    <a class="{{ cat_class }}" href="{{ post.url }}">{{ post.title }}</a> 
{% endfor %} 
+0

非常感謝您的解釋,併爲我可憐的英語感到抱歉。我在帖子循環中添加了一個for循環,但Jekyll會多次生成每個鏈接。 – Peiwen

+0

嗨@Peiwen使用#2以上。 –

+0

#2作品完美!非常感謝你。只是好奇,如果它很容易使用類名稱,如cate1,cate2,cate3而不是類別名稱?這將更符合我的jQuery代碼。如果不是,我會在jquery中找到一些解決方法。感謝 – Peiwen

相關問題