gpt4 book ai didi

tags - Jekyll 和 Liquid - 按相等标签数量 >= 2 显示相关帖子

转载 作者:行者123 更新时间:2023-12-01 16:04:46 24 4
gpt4 key购买 nike

我想在每个帖子的底部添加一个名为“相关帖子”的栏,并且帖子相关和出现的标准应该是中至少有 2 个相同的标签两篇文章

到目前为止我的方法:

{% for tag in page.tags %}

{% assign currentTag = tag | first %}


{% for post in site.posts | limit:3 %}
{% if post.tags contains currentTag | plus:1 %}

<div>
<a href="{{ post.url }}">
<img src="{{site.baseurl}}/asset/img/{{ post.img-thumb }}">
</a>
<h5> {{ post.title }}</h5>
</div>


{% endif %}
{% endfor %}

{% endfor %}

感谢您的帮助!

最佳答案

这段代码可以解决问题:

<div class="relatedPosts">

<h3>Related post</h3>

{% comment %}---> the maximum number of related to posts
to be printed {% endcomment %}
{% assign maxRelated = 5 %}

{% comment %}---> the minimum number of common tags
to have for a post to be considered
as a related post {% endcomment %}
{% assign minCommonTags = 3 %}

{% assign maxRelatedCounter = 0 %}

{% for post in site.posts %}

{% assign sameTagCount = 0 %}
{% assign commonTags = '' %}

{% for tag in post.tags %}
{% comment %}---> Only compare if post is
not same as current page {% endcomment %}
{% if post.url != page.url %}
{% if page.tags contains tag %}
{% assign sameTagCount = sameTagCount | plus: 1 %}
{% capture tagmarkup %} <span class="label label-default">{{ tag }}</span> {% endcapture %}
{% assign commonTags = commonTags | append: tagmarkup %}
{% endif %}
{% endif %}
{% endfor %}

{% if sameTagCount >= minCommonTags %}
<div>
<h5><a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}{{ commonTags }}</a></h5>
</div>
{% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}
{% if maxRelatedCounter >= maxRelated %}
{% break %}
{% endif %}
{% endif %}

{% endfor %}

</div>

编辑:添加了 maxRelatedminCommonTags 的“配置”,以及避免将帖子放入自己的相关帖子列表中的测试。

关于tags - Jekyll 和 Liquid - 按相等标签数量 >= 2 显示相关帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25348389/

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