如何在 Jekyll 中通过发布标签列出最近的帖子?

How do I list the recent postings by posting tag in Jekyll?

在我的 Jekyll 页面上,我有三种不同类型的帖子:游戏、商业、思想,每种帖子类型都按标签分类。

我将下面的代码用于我的 index.html,因为它通过发布时间显示我网站上的最新帖子,并在列表中显示带有 'post.title'、'post.date' 的每个帖子, 'post.tags'、'page.summary(content)' 信息。

但是,我的网站有不同的页面用于不同的发布类别(游戏、商业评论和想法),我想使用我在 index.html(下面的代码)中使用的这种显示格式,在每个不同页面(游戏、商业评论和想法)的首页。

<div class="home">

    <div class="post-list">
        {% for post in site.posts limit:10 %}


    <h2><a class="post-link" href="{{ post.url | remove: "/" }}">{{ post.title }}</a></h2>
        <span class="post-meta">{{ post.date | date: "%b %-d, %Y" }} /
            {% for tag in post.tags %}

                <a href="{{ "tag_" | append: tag | append: ".html"}}">{{tag}}</a>{% unless forloop.last %}, {% endunless%}

                {% endfor %}</span>
        <p>{% if page.summary %} {{ page.summary | strip_html | strip_newlines | truncate: 160 }} {% else %} {{ post.content | truncatewords: 50 | strip_html }} {% endif %}</p>

        {% endfor %}

    </div>
</div>

为了更清楚地理解,我附上了一些可能有助于理解我的问题的图片。

非常感谢社区的提前帮助!


index.html showing recent postings(of all kinds) from the sites

I want the below listing to show the recent postings only from the postings with game tags.

您的循环当前遍历所有标签的前十个帖子:

{% for post in site.posts limit:10 %}

您可以使用 "game" 标签循环访问前十个帖子,方法是首先过滤那些帖子:

{% assign game_posts = site.posts | where_exp: "post", "post.tags contains 'game'" %}
{% for post in game_posts limit:10 %}

where_exp 过滤器至少需要 Jekyll v3.2.0。

由于您打算为每个标签重复使用该标记块,我建议您考虑将其转换为布局,并将标签作为使用它的每个页面的前端变量。