我如何在Jekyll中获得相关职位?

我如何在Jekyll中获得相关职位?,jekyll,liquid,Jekyll,Liquid,我想修改找到的答案,但它似乎对我不起作用,因为它只会导致“没有相关帖子”的重复声明 最初,当没有相关帖子时,不会显示任何内容,我想对此进行更改。同样,在我添加的else语句中,它只显示了3次“没有相关帖子”,我似乎不明白为什么 任何帮助都将不胜感激 {% assign maxRelated = 0 %} {% assign minCommonTags = 3 %} {% assign maxRelatedCounter = 0 %} {% for post in site.posts %

我想修改找到的答案,但它似乎对我不起作用,因为它只会导致“没有相关帖子”的重复声明

最初,当没有相关帖子时,不会显示任何内容,我想对此进行更改。同样,在我添加的else语句中,它只显示了3次“没有相关帖子”,我似乎不明白为什么

任何帮助都将不胜感激

{% assign maxRelated = 0 %}

{% assign minCommonTags =  3 %}

{% assign maxRelatedCounter = 0 %}

{% for post in site.posts %}

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

    {% for tag in post.tags limit:1 %}

    {% if post.url != page.url %}
        {% if page.tags contains tag %}
        {% assign sameTagCount = sameTagCount | plus: 1 %}
        <a href="..{{ post.url | relative_url }}">{{ post.title }}</a>
        <p>{{ post.excerpt | strip_html | truncatewords:80 }}</p>
        {% assign commonTags = commonTags | append: tagmarkup %}
        {% else %}
        There are no related posts.
        {% endif %}

    {% endif %}
    {% endfor %}

    {% if sameTagCount >= minCommonTags %}

    {% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}
    {% if maxRelatedCounter >= maxRelated %}
        {% break %}
    {% endif %}
    {% endif %}

{% endfor %}
{%assign maxRelated=0%}
{%assign minCommonTags=3%}
{%assign maxRelatedCounter=0%}
{site.posts%中的post为%s}
{%assign sameTagCount=0%}
{%assign commonTags=''%}
{post.tags限制中的标记为%1%}
{%if post.url!=page.url%}
{%如果page.tags包含标记%}
{%assign sameTagCount=sameTagCount | plus:1%}
{{post.extract | strip | html | truncatewords:80}

{%assign commonTags=commonTags | append:tagmarkup%} {%else%} 没有相关职位。 {%endif%} {%endif%} {%endfor%} {%if sameTagCount>=minCommonTags%} {%分配maxRelatedCounter=maxRelatedCounter |加:1%} {%如果maxRelatedCounter>=maxRelated%} {%break%} {%endif%} {%endif%} {%endfor%}
在链接的答案中没有else块和out,它只生成CommonTag

{% 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 %}
{%for post.tags%}
{%comment%}->仅当post为
与当前页面不同{%endcomment%}
{%if post.url!=page.url%}
{%如果page.tags包含标记%}
{%assign sameTagCount=sameTagCount | plus:1%}
{%capture tagmarkup%}{{tag}{%endcapture%}
{%assign commonTags=commonTags | append:tagmarkup%}
{%endif%}
{%endif%}
{%endfor%}
然后,如果sameTagCount大于或等于minCommonTags,它将输出一个链接作为h5

{% if sameTagCount >= minCommonTags %}
   <div>
     <h5><a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}{{ commonTags }}</a></h5>
   </div>
...
{%if sameTagCount>=minCommonTags%}
...

在您的例子中,ìt输出“没有相关帖子”。如果条件
{%if page.tags包含tag%}
不正确,这显然会发生三次。

如何使它只输出一次而不是三次?也许您可以使用类似
{%assign norrelatedposts=“没有相关帖子”。%%
并将其输出为整个for块外部的
{{norrelatedposts}