2014-01-06 29 views
13

我以某種方式被卡住,並沒有找到正確的方式來做到這一點。Jekyll&Liquid:產出類別列表和發佈數量?

我想輸出一個類別列表與每個類別的帖子數量。

我能走到今天:https://paste.xinu.at/dOLtod/

,但我從來沒有設法得到真正的計數。我嘗試了很多方法,沒有任何工作,例如通過每個帖子並檢查每個類別是否等於{{category |第一}}。

這裏是不計數的代碼:

<ul class="dropdown-menu"> 
    {% for category in site.categories %} 
     <li class="camelize_me"> 
      <a href="/tags/{{category | first}}/"> 
       {{category | first }} 
       <span class="badge"> 
       <!-- Post count here --> 
       </span> 
      </a> 
     </li> 
    {% endfor %} 
</ul> 

有沒有人一個想法,得到這個工作?

+2

如果您有答案,請將其作爲答案發布,而不是編輯。 – Darkhogg

+0

在我認爲是因爲我的名譽後的8小時後,我無法做到這一點,因爲我是新來的,所以我沒有。我試過了,我會在稍後添加它作爲答案。 ;) – koffeingeladen

回答

22

我已經寫了一個代碼片段,不僅顯示每個類別的帖子數,還導航到一個特定類別的帖子被點擊。我希望你覺得它有幫助:

<ul class="tag-box inline"> 
{% assign tags_list = site.categories %} 
    {% if tags_list.first[0] == null %} 
    {% for tag in tags_list %} 
     <li><a href="#{{ tag }}">{{ tag | capitalize }} <span>{{ site.tags[tag].size }}</span></a></li> 
    {% endfor %} 
    {% else %} 
    {% for tag in tags_list %} 
     <li><a href="#{{ tag[0] }}">{{ tag[0] | capitalize }} <span>{{ tag[1].size }}</span></a></li> 
    {% endfor %} 
    {% endif %} 
{% assign tags_list = nil %} 
</ul> 

{% for tag in site.categories %} 
    <h2 id="{{ tag[0] }}">{{ tag[0] | capitalize }}</h2> 
    <ul class="post-list"> 
    {% assign pages_list = tag[1] %} 
    {% for post in pages_list %} 
     {% if post.title != null %} 
     {% if group == null or group == post.group %} 
     <li><a href="{{ site.url }}{{ post.url }}">{{ post.title }}<span class="entry-date"><time datetime="{{ post.date | date_to_xmlschema }}" itemprop="datePublished">{{ post.date | date: "%B %d, %Y" }}</time></a></li> 
     {% endif %} 
     {% endif %} 
    {% endfor %} 
    {% assign pages_list = nil %} 
    {% assign group = nil %} 
    </ul> 
{% endfor %} 
+0

有沒有什麼方法可以對類別進行排序?我試過'{%assign tags_list = site.categories |排序:「標題」%}'但沒有成功 –

+0

上面的腳本將工作在jekyll3中嗎? – Sunil

12

解決方案:{{ category | last }}擁有我的所有帖子,因此{{ category | last | size }}顯示計數。我在IRC上得到了幫助。 :)

+0

這工作!我沒有從Liquid Document找到任何解決方案。但我在這裏找到它!哥們,謝啦! – shen

1

對侯賽因的答案進行了逐步改進,對類別進行排序。使用Jekyll 3.3.1測試:

<h1 class='tag'>Blog Posts Sorted By Category</h1> 
{% assign sorted_categories = site.categories | sort {|left, right| left[0] <=> right[0]} %} 
{% for tag in sorted_categories %} 
    <h2 class='tag' id="{{ tag[0] }}">{{ tag[0] | capitalize }}</h2> 
    <ul class="post-list"> 
    {% assign pages_list = tag[1] %} 
    {% for post in pages_list %} 
     {% if post.title != null %} 
     {% if group == null or group == post.group %} 
     <li><a href="{{ site.url }}{{ post.url }}"> 
      <span class="entry-date"><time datetime="{{ post.date | date_to_xmlschema }}" itemprop="datePublished">{{ post.date | date: "%B %d, %Y" }}</time></span> 
      &bull; 
      {{ post.title }} 
     </a></li> 
     {% endif %} 
     {% endif %} 
    {% endfor %} 
    {% assign pages_list = nil %} 
    {% assign group = nil %} 
    </ul> 
{% endfor %} 
+0

我認爲重要的是要提到這是一個區分大小寫的排序,大寫在小寫之前。所以如果你有蘋果,香蕉和斑馬的分類,那麼它將被分類爲「蘋果,斑馬,香蕉」,這可能不是你想要的。 – kimbaudi

+0

這是真的。我注意在我的標籤中使用所需的大小寫。例如:AWS,AI和所有其他標籤都被大寫。效果很好! –

相關問題