杰基尔& Liquid:输出类别列表,包含帖子数量?

时间:2014-01-06 08:35:22

标签: 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>

有没有人想要完成这项工作?

3 个答案:

答案 0 :(得分:23)

我编写了一个代码段,不仅显示了每个类别中的帖子计数,还导航到了点击的特定类别的帖子。我希望你觉得它有用:

<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 %}

答案 1 :(得分:13)

解决方案:{{ category | last }}包含我的所有帖子,因此{{ category | last | size }}会显示计数。我得到了IRC的帮助。 :)

答案 2 :(得分:1)

对Hossain的答案进行了逐步改进,对答案进行了分类。用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 %}