【问题标题】:jekyll + liquid: number of posts by monthjekyll + Liquid:按月发布的帖子数
【发布时间】:2015-03-24 12:21:56
【问题描述】:

我在 jekyll 中学习流媒体,但我很难按月统计帖子数量。计算每个标签或类别的帖子数量似乎很容易(因为有变量 site.tags 和 site.categories),我对此没有任何问题。这里是my live example,用于计算每个标签/类别的帖子的源代码可在github 上找到。为了按月计算帖子,我尝试使用像

这样的计数器
{% capture counter %}{{ counter | plus:1 }} {% endcapture %}   {% endif %}

但它的各种用途并没有给我预期的计数,我现在怀疑有更好的方法。问题是我如何修改下面的代码,使其显示月份(给定年份)而不是每个类别的帖子数?

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

【问题讨论】:

标签: count jekyll liquid


【解决方案1】:

复制自the source code of my blog,稍作修改:

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

生成的 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>

替代方案:

要按年而不是按月分组,请将其出现的所有三个位置的 %B %Y 更改为 %Y
%B 是完整的月份名称,%Y 是年份,见documentation)

使用%Y,生成的 HTML 将如下所示:

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

(这是我在博客上使用的)

【讨论】:

  • 谢谢。你的回答促使我更长时间地解决我的问题。
【解决方案2】:

仅柜台只是答案的一半。与按标签或类别对帖子进行分组一样,需要在按月/年分组的帖子列表中加入一个计数器。我终于找到了一些似乎正在工作的东西

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

上面应该有改进的方法,所以我暂时不接受任何答案。很高兴看到有更多经验的人提供更优雅的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多