Jekyll & Liquid: Output category list with post count?

前端 未结 3 1806
逝去的感伤
逝去的感伤 2021-02-01 07:42

I\'m somehow stuck and don\'t find the right way to do it.

I want to output a category list with the number of posts in each categorie.

I got this far: https://p

相关标签:
3条回答
  • 2021-02-01 08:09

    An incremental improvement over Hossain's answer, which sorts the categories. Tested with Jekyll 3.3.1:

    <h1 class='tag'>Blog Posts Sorted By Category</h1>
    {% assign sorted_categories = site.categories | sort %}
    {% 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 讨论(0)
  • 2021-02-01 08:17

    I have written a code snippet that not only shows post count in each category, but also navigates to the posts of a specific category which got clicked. I hope you find it helpful:

    <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 讨论(0)
  • 2021-02-01 08:24

    Solution: {{ category | last }} has all my posts, so {{ category | last | size }} displays the count. I got help on the IRC. :)

    0 讨论(0)
提交回复
热议问题