Search code examples
jekyllliquidoffsetposts

How to ignore offset in Jekyll when previous post is skipped


I'm trying to create my first blog on Jekyll. I'm stuck on one thing: I have a section for one of my categories, let's say "news":

<section class="news">
    <div class="container">
        <div class="row no-gutters">
        
{% for post in site.categories.news limit: 2 offset: 0 %} 
{% include news-item-col-6.html %}
{% endfor %}

{% for post in site.categories.news limit: 3 **offset: 2** %}
{% include news-item-col-4.html %}
{% endfor %}
            
        </div>
    </div>
</section>

news-item-col-6:

{% if post.thumb != 0 %}
   <div class="col-md-6">
        <div class="pattern">
            <div class="overlay item-title" style="background-image: url({{ post.thumb }});">               
                <div class="item-title-content">
                    <h3><a href="{{ post.url }}">{{ post.header }}</a></h3>                     
                </div>
            </div>      
        </div>
    </div>
{% endif %}

news-item-col-4:

{% if post.thumb != 0 %}
  <div class="col-md-4">
    <div class="pattern">           
        <div class="overlay item-title" style="background-image: url({{ post.thumb }});">
            <div class="item-title-content">
                <h3><a href="{{ post.url }}">{{ post.header }}</a></h3>                 
            </div>
        </div>                  
    </div>
  </div>
{% endif %}

my posts tp

---
layout: post
title: title | site.com
header: title
description: description
categories: categories url
catname: News
image: "images/URL /to image/1.jpg"
thumb: "images/URL /to thumb/1t.jpg"
permalink: "blog/:categories/:year-:month-:day-:slug.html"
---

The problem is that not all of my posts will have background thumb, and all I want to do is to ignore the posts which has no post.thumb. and the code is works, but unfortunately col-md-4 block's offset is not ignoring post's order with no post.thumb.

The pictures below shows what I want:

So what should I do to make it work right?


Solution

  • Just use a custom counter, like this:

    {% assign counter = 0 %} <!-- create a custom counter and set it to zero -->
    {% for post in site.categories.news %} <!-- loop through the posts in news -->
      {% if post.thumb %} <!-- check if the post has a thumbnail -->
        {% assign counter = counter | plus: 1 %} <!-- increment the counter if it does -->
        {% if counter < 3 %} <!-- if this is the first or second counted post -->
          {% include news-item-col-6.html %} <!-- include the col-6 element -->
        {% elsif counter < 6 %} <!-- else -->
          {% include news-item-col-4.html %} <!-- include the col-4 element -->
        {% endif %}
      {% endif %}
    {% endfor %}