andrekos
andrekos

Reputation: 2892

jekyll + liquid: number of posts by month

I am learning liquid within jekyll and I am having difficulties capturing the number of posts by month. It appears easy to count the number of posts per tag or category (as there are variables site.tags and site.categories), with which I have no problems. Here is my live example, and the source code for counting posts per tag / category is available on github. For counting posts by month, I tried to use a counter like

{% capture counter %}{{ counter | plus:1 }} {% endcapture %}   {% endif %}

but various uses of it did not give me the expected count and I now suspect there is a better approach. The question is how I would possibly modify the code below so it displays the number of posts in the month (of a given year) rather than per category?

{% capture site_cats %}{% for cat in site.categories %}{{ cat | first }}
{%unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %}
{% assign sortedcats = site_cats | split:',' | sort %}


{% for category in sortedcats %}
{{category }}{{site.categories[category] | size }}
<ul>
  {% for post in site.categories[category] %}
  {% if post.url %}
    <li><a href="{{ post.url }}">{{ post.title }}</a>
    <time> &mdash; {{ post.date | date: "%a %e-%b-%Y" }}</time> 
    </li>
    {% endif %}
  {% endfor %}
</ul>
{% endfor %}

Upvotes: 3

Views: 2954

Answers (2)

andrekos
andrekos

Reputation: 2892

The counter alone was only half the answer. As with grouping posts by tag or category, it was required to incorporate a counter in the list of posts grouped by month/year. I've finally arrived at something that appears to be working

{% for post in site.posts %}
 {% assign thisyear = post.date | date: "%B %Y" %}
 {% assign prevyear = post.previous.date | date: "%B %Y" %}
 {% assign counter = counter | plus: 1 %}

  {% if thisyear != prevyear %}
<h2 class="archive"><a>{{ thisyear }}</a> <i class="fa fa-cube"></i>  
      {{counter }}</h2>

<ul>
  {% assign fli = forloop.index | minus: counter %}
  {% for post2 in site.posts limit: counter offset: fli %} 
    <li><a href="{{ post2.url }}">{{ post2.title }}</a> 
    <time> &mdash; {{ post2.date | date: "%a %e-%b-%Y"}}</time> 
    </li>
  {% endfor %}
</ul>   
 {% assign counter = 0 %}
  {% endif %}
{% endfor %}

There should be a way to improve on the above, so I won't accept any answer for now. It'd be great to see a more elegant solution from those with more experience.

Upvotes: 0

Christian Specht
Christian Specht

Reputation: 36451

Copied from the source code of my blog, slightly modified:

{% assign counter = 0 %}
{% for post in site.posts %}
  {% assign thisyear = post.date | date: "%B %Y" %}
  {% assign prevyear = post.previous.date | date: "%B %Y" %}
  {% assign counter = counter | plus: 1 %}
  {% if thisyear != prevyear %}
    <li><a href="/archive/#{{ post.date | date:"%B %Y" }}">{{ thisyear }} ({{ counter }})</a></li>
    {% assign counter = 0 %}
  {% endif %}
{% endfor %}

The generated HTML:

<li><a href="/archive/#January 2015">January 2015 (1)</a></li>
<li><a href="/archive/#November 2014">November 2014 (2)</a></li>
<li><a href="/archive/#October 2014">October 2014 (1)</a></li>
<li><a href="/archive/#September 2014">September 2014 (1)</a></li>

Alternative:

To group by year instead of by month, change %B %Y to %Y in all three places where it occurs.
(%B is the full month name and %Y is the year, see the documentation)

With %Y, the generated HTML will look like this:

<li><a href="/archive/#2015">2015 (1)</a></li>
<li><a href="/archive/#2014">2014 (8)</a></li>
<li><a href="/archive/#2013">2013 (11)</a></li>
<li><a href="/archive/#2012">2012 (5)</a></li>

(this is what I'm using on my blog)

Upvotes: 6

Related Questions