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

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

问题描述

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

到目前为止我的方法:

{% for tag in page.tags %}{% 分配 currentTag = 标签 |第一的 %}{% for post in site.posts |限制:3 %}{% 如果 post.tags 包含 currentTag |加:1%}

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

感谢您的帮助!

解决方案

这段代码可以解决问题:

<div class="relatedPosts"><h3>相关帖子</h3>{% 评论 %}--->相关帖子的最大数量待打印 {% endcomment %}{% 分配 maxRelated = 5 %}{% 评论 %}--->公共标签的最小数量有一个职位被考虑作为相关帖子 {% endcomment %}{% 分配 minCommonTags = 3 %}{% 分配 maxRelatedCounter = 0 %}{% for post in site.posts %}{% 分配sameTagCount = 0 %}{% 分配 commonTags = '' %}{% for tag in post.tags %}{% 评论 %}--->仅比较 post 是与当前页面不同 {% endcomment %}{% if post.url != page.url %}{% if page.tags 包含标签 %}{% 分配sameTagCount = sameTagCount |加:1%}{% capture tagmarkup %} <span class="label label-default">{{ tag }}</span>{% 结束捕获 %}{% 分配 commonTags = commonTags |附加:标记标记 %}{% 万一 %}{% 万一 %}{% endfor %}{% if sameTagCount >= minCommonTags %}

<h5><a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}{{ commonTags }}</a></h5></div>{% 分配 maxRelatedCounter = maxRelatedCounter |加:1%}{% if maxRelatedCounter >= maxRelated %}{% 休息 %}{% 万一 %}{% 万一 %}{% endfor %}</div>

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

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.

My approach so far:

{% 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 %}

Thanks for your help!

解决方案

This code does the trick :

<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>

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

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

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