使用自定义 post.variable 计数帖子

Count posts with custom post.variable

我使用 Hexo hexo.io,但我想 Jekyll 用户可能也知道这个,因为它很相似。

我的 custom.post.variablefruit。 所以我的 .md 文件有:

title: food
fruit: apple

title: more food
fruit: banana

title: still more food
fruit: banana

title: try some food
fruit: banana

title: enjoy food
fruit: apple

如何计算fruit: apple的所有帖子? 我知道我的自定义 post.variable 不是 site 范围的,所以这是我的问题。 我试图将我的 post.fruit 变量作为一个函数放入 site.posts.length 中,但这没有用。

使用 Liquid 遍历 post 的列表。每当 post 将 fruit 字段设置为 apple,然后递增计数器。

{% assign postCount = 0 %}
{% for post in site.posts %}
    {% if post.fruit == "apple" %}
        {% assign postCount = postCount | plus: 1 %}
    {% endif %}
{% endfor %}

The number of posts with 'fruit: apple' is {{postCount}}