2016-10-27 24 views
1

我試圖循環通過已添加到收集帖子的類別。對於默認的'帖子'部分,它很容易:如何循環通過Jekyll集合中的類別

{% for category in site.categories %} 
    {{ category }} 
{% endfor %} 

但我似乎無法得到我的收藏這工作。我認爲這將是沿線:

{% for category in my_collection.categories %} 
    {{ category }} 
{% endfor %} 

但這似乎並沒有工作。任何幫助,將不勝感激。

回答

1

對於任何需要這個答案的人...我已經設法解決這個問題,通過添加所有獨特的'my_collection'類別到數組然後循環。下面的代碼:

<!-- create categories array--> 
{% assign categories_array = "" | split:"|" %} 

<!--Add each unique 'my_collection' category to the array--> 
{% for post in site.my_collection %} 
    {% for category in post.categories %} 
     {% assign categories_array = categories_array | push: category | uniq %} 
    {% endfor %} 
{% endfor %} 

<!--Output the categories--> 
{% for category in categories_array %} 
    {{ category }} 
{% endfor %} 
2

你可以抓住每一個類別的名稱,像這樣:

{% for category in site.categories %} 
    {{ category | first | strip_html }} 
{% endfor %}