【发布时间】:2021-05-13 12:24:55
【问题描述】:
有一个 Liquid 过滤器sample 允许您“从数组中选择一个随机值。可选地,选择多个值”。 (参见文档here)
我的用例是有一个照片日记供查看者点击并从我现有的收藏“照片日记”中生成一个随机帖子,而不是在下次点击时重复该帖子。
这是我的代码:
{% assign posts = site.photodiary | where_exp:"post", "post.url != page.url" | sample: 8 %}
{% for post in posts limit:1 %}
<a class="prev" href="{{post.url}}">Load a post</a>
{% endfor %}
</div>
<main>
{%- if page.content-type == "photodiary" -%}
<h2>{{page.title}}</h2>
<p> {{content}} </p>
{%- endif -%}
</main>
目前我只有 8 个条目,因此是 sample: 8。
虽然代码有效,但点击 1-2 次后,帖子将重复,我的页面只会加载相同的 2 到 3 个帖子。
如果我的逻辑或代码有缺陷,谁能告诉我? 非常感谢!
【问题讨论】:
标签: jekyll github-pages liquid