skip to Main Content

What I’m trying to accomplish is when a user is on an individual blog article/post, I want to display unique “related articles” based off of matching tags.

Here’s what I have so far:

{% for tag in article.tags %}

  {% assign counter = 0 %}
  {% for article in blog.articles %}
    {% if article.tags contains tag and counter < 2 %}
      {% assign counter = counter | plus: 1 %}
      <li class="well">
      {% if article.excerpt.size > 0 %}
          <div class="thumb-article">
              <a href="{{ article.url }}">
                  {{ article.excerpt }}
              </a>
          </div>
          {% endif %}
              <h3><a href="{{ article.url }}">{{ article.title }}</a></h3>
              <p>{{ article.content | strip_html | strip_newlines | truncatewords: 40 }}</p>
      </li>
    {% endif %}
  {% endfor %}

{% endfor %}

Surprisingly, (to me since this is my first experience with Shopify and liquid) it works, just a little too well as it gets duplicate posts.

Is there some way I can keep it from getting duplicate articles?

2

Answers


  1. This thread has what you need: Shopify liquid get related blog posts

    It creates an empty related posts variable then defines it in a loop that looks through other posts of that same category. To repeat its answer:

        {% assign related_posts = "" %}
    {% for article in blogs.blog.articles %}
      {% if article.tags contains product.handle %}
        {% capture post %}
          <li><a href="{{ article.url }}"><p>{{ article.title }}</p></a></li>
        {% endcapture %}
        {% assign related_posts = related_posts | append:post %}
      {% endif %}
    {% endfor %}
    {% if related_posts.size > 0 %}
      <ul> {{ related_posts }} </ul>
    {% else %}
      No related posts!
    {% endif %}
    

    Go to that link above to see the full response.

    Login or Signup to reply.
  2. For those who also searching how to implement related blog posts to a blog not product like in the links provided, there is the fix for code above:

    ...
    {% assign skip_articles = article.handle | split: '.....' %}
    ...
    {% for ...
        {% if ...
          {% unless skip_articles contains related_article.handle %}
            ...
            {% assign temp = related_article.handle | split: '.....' %}
            {% assign skip_articles = skip_articles | concat: temp %}
            ...
    

    split by anything you wouldn’t find in handle to create array

    ending up with something like this:

    <div class='relatedArticles'>
      {% for tag in article.tags %}
    
      {% assign counter = 0 %}
      {% assign skip_articles = article.handle | split: '.....' %}
      {% for related_article in blog.articles %}
        {% if related_article.tags contains tag and counter < 6 %}
          {% unless skip_articles contains related_article.handle %}
            {% assign counter = counter | plus: 1 %}
            {% assign temp = related_article.handle | split: '.....' %}
            {% assign skip_articles = skip_articles | concat: temp %}
            <div class="well">
              <h3><a href="{{ related_article.url }}">{{ related_article.title }}</a></h3>
              {% if related_article.excerpt.size > 0 %}
                <p>{{ related_article.excerpt }}</p>
              {% else %}
                <p>{{ related_article.content | truncatewords: 40 }}</p>
              {% endif %}
            </div>
          {% endunless %}
        {% endif %}
      {% endfor %}
    
      {% endfor %}
    </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search