and*_*kos 3 count liquid jekyll
我正在jekyll内学习流动性,并且很难按月获取职位数量。似乎很容易计算每个标签或类别的帖子数(因为有变量site.tags和site.categories),而我没有遇到任何问题。这是我的实时示例,可以在github上获得用于统计每个标签/类别的帖子的源代码。为了按月计数帖子,我尝试使用类似
{% capture counter %}{{ counter | plus:1 }} {% endcapture %} {% endif %}
Run Code Online (Sandbox Code Playgroud)
但是它的各种用法并没有给我预期的效果,我现在怀疑有更好的方法。问题是我怎么可能修改下面的代码,以便显示月份(给定年份)而不是每个类别的帖子数?
{% 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> — {{ post.date | date: "%a %e-%b-%Y" }}</time>
</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
从我的博客的源代码复制,稍作修改:
{% 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 %}
Run Code Online (Sandbox Code Playgroud)
生成的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>
Run Code Online (Sandbox Code Playgroud)
选择:
要按年份而不是按月分组,%B %Y请%Y在发生的所有三个地方都更改为。
(%B是完整的月份名称,%Y是年份,请参阅文档)
使用%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>
Run Code Online (Sandbox Code Playgroud)
(这就是我在博客上使用的内容)
| 归档时间: |
|
| 查看次数: |
2357 次 |
| 最近记录: |