Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll & Liquid: Output category list with post count?

Tags:

jekyll

liquid

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://paste.xinu.at/dOLtod/

but I never managed to get the real count. I tried so many ways and nothing worked, for example going through each post and checked on every category if it equals {{ category | first }}.

Here is the code without the count:

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

Has anyone an idea to get this done?

like image 651
koffeingeladen Avatar asked Jan 06 '14 08:01

koffeingeladen


People also ask

What is Jekyll used for?

Jekyll is a static site generator. It takes text written in your favorite markup language and uses layouts to create a static website. You can tweak the site's look and feel, URLs, the data displayed on the page, and more.

What language is Jekyll?

Jekyll is a free and open-source application written in the Ruby programming language. Thousands of websites, including the Markdown Guide, rely on Jekyll to convert Markdown source files to HTML output. GitHub Pages uses Jekyll as the backend for its free website creation service.

Do people still use Jekyll?

Its growth has not been as fast as a few other, newer SSGs in recent years, but it remains a very popular choice. Jekyll was created to simplify website hosting and infrastructure management, mostly by getting rid of it.

Who created Jekyll?

History. Jekyll was first released by Tom Preston-Werner in 2008. Jekyll was later taken over by Parker Moore, who led the effort in releasing Jekyll 1 and has been the new maintainer since then. Jekyll started a web development trend towards static websites.


2 Answers

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 %}
like image 174
Hossain Mohd Faysal Avatar answered Dec 25 '22 19:12

Hossain Mohd Faysal


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

like image 22
koffeingeladen Avatar answered Dec 25 '22 18:12

koffeingeladen