carolineschnapp
2/21/2012 - 6:20 PM

Pulling relevant articles from one blog in product.liquid

Pulling relevant articles from one blog in product.liquid

{% comment %}

Put the code below in a new snippet called relevant-articles.liquid. Include that snippet in product.liquid using {% include 'relevant-articles' %}.

Edit how many articles maximum you want to show by editing the how_many_articles_to_show variable assignment near the top.

Requirement: whenever you are blogging and you happen to say something that is relevant to product A or product B, add the title of those products as tags to your blog post.
For example, if you are writing about how you have been working on improving a product called 'Gizmo', then add 'Gizmo' as tag to your blog post.

{% endcomment %}

{% assign blog = blogs.news %}
{% assign how_many_articles_to_show = 3 %}
{% assign index = 0 %}
{% paginate blog.articles by 100 %}
{% for article in blog.articles %}
  {% if article.tags contains product.title and index < how_many_articles_to_show %}
    {% assign index = index | plus: 1 %}
    <div class="article">
      <h4>{{ article.title }}</h4>
      <div>
      {% if article.excerpt.size > 0 %}
        {{ article.excerpt }}
      {% else %}
        <p>{{ article.content | strip_html | truncatewords: 50 }}</p>
      {% endif %}
      <a href="{{ article.url }}">Read more &rarr;</a>
      </div>
    </div>
  {% endif %}
{% endfor %}
{% endpaginate %}