gpt4 book ai didi

ruby - 在 Jekyll 中过滤 site.related_posts

转载 作者:数据小太阳 更新时间:2023-10-29 07:05:47 26 4
gpt4 key购买 nike

我对 Jekyll 和 Ruby 很陌生(但是,非常兴奋)。

在不使用插件的情况下,我试图找到一种方法来过滤 site.related_posts

例如,我正在阅读标题为 Foo 且类别为 A、B 的帖子。

该站点总共包含 3 个帖子:

  1. Foo(类别:A、B)
  2. 条形图(类别:A、C、D)
  3. 动物园(类别:B、F)

默认情况下,在 Jekyll 中我们这样做:

{% for post in site.related_posts limit:5 %}
{% endfor %}

但是,上面的代码返回所有 (3) 个帖子。

一个帖子包含很多类别,所以类别应该是一个数组。

如何修改代码并仅返回类别与当前帖子类别相交的类别?

(在此示例中,我希望代码仅返回 FooZoo。)

最佳答案

我现在无法对此进行测试,但在给定 Liquid's 的情况下,这样的事情会起作用语法限制:

{% for post in site.related_posts limit:5 %}
{% assign match = false %}
{% for category in post.categories %}
{% if page.categories contains category %}
{% assign match = true %}
{% endif %}
{% endfor %}
{% if match %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endif %}
{% endfor %}

关于ruby - 在 Jekyll 中过滤 site.related_posts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10906574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com