2
votes

I'm trying to create an "Also in the blog" iteration of blog posts. I'm happy to just show the latest posts, but can't figure out how I'd go about excluding the current post the user is viewing from this iteration.

Here is my code (simplified):

{% for article in blog.articles limit:3 %}

  <div>  
    {{ article | img_url: '1024x1024' | img_tag: article.title }}
    {{ article.excerpt }}
    {{ article.content | strip_html | truncatewords: 20 }}
  </div>

{% endfor %}
2

2 Answers

6
votes

If you're on an article page you'd want something like:

    {% assign currentHandle = article.handle %}
    {% assign articlesFound = 0 %}
    {% for addin in blogs[blog.handle].articles %}
      {% unless addin.handle == currentHandle %}
       <div><a href="{{ addin.url }}">{{ addin.title }}</a></div>
       {% assign articlesFound = articlesFound |plus: 1 %}
       {% if articlesFound == 3 %}{% break %}{% endif %}
      {% endunless %}
    {% endfor %}
1
votes

By using the current page title? And checking if it contains the title of the current blog post.

{% for article in blog.articles limit:3 %}
    {% unless page_title contains article.title %}
    <div>
        ...
    </div>
    {% endunless %}
{% endfor %}