Jekyll and liquid-按等号> = 2的数量显示相关帖子 [英] Jekyll and liquid - Show related posts by amount of equal tags >= 2

查看:53
本文介绍了Jekyll and liquid-按等号> = 2的数量显示相关帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个帖子的底部添加一个名为相关帖子"的栏,并且要关联和显示帖子的条件应该是,其中至少有个相等数量的标签这两个帖子.

i'd like to add a bar called "related posts" at the bottom of each post and the criteria for posts to be related and to appear there should be that there is an amount of minimum 2 equal tags in both posts.

到目前为止我的方法:

{% 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添加了配置",并进行了一项测试,以避免将帖子放入自己相关的帖子列表中.

Edit : Added 'configuration' for maxRelated and minCommonTags, plus a test to avoid putting a post in is own related post list.

这篇关于Jekyll and liquid-按等号&gt; = 2的数量显示相关帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆