Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll every 4th item in loop display differently

I am using jekyll to populate my blog page with blog posts. I want there to be two posts for every div with the "row" class, but I also want every 4th item to be an ad (skip over the post and move to the next row but still include something else that's not a post).

So if there are 6 posts, the output should look like this

<div class="row"> <!-- 1st row -->
  <div> {{ post.title }} </div>  <!-- 1st post, no skip -->
  <div> {{ post.title }} </div>  <!-- 2nd post, no skip -->
</div>

<div class="row"> <!-- 2nd row -->
  <div> {{ post.title }} </div>  <!-- 3rd post, no skip -->
  <div> THIS IS NOT A POST </div>  <!-- skip post 4, put something else -->
</div>

<div class="row"> <!-- 3rd row -->
  <div> {{ post.title }} </div>  <!-- 4th post, because last item was skipped to display something else -->
  <div> {{ post.title }} </div>  <!-- 5th post, no skip -->
</div>

<div class="row"> <!-- 4th row -->
  <div> {{ post.title }} </div>  <!-- 6th post, no  skip -->
</div>

<!-- and so on, so every 4th item is not a post, but the posts continue after the skipped post -->

I have the post loop part but I cant figure out how to add the SKIP

{% assign rows = site.posts.size | divided_by: 2.0 | ceil %}
{% for i in (1..rows) %}
  {% assign offset = forloop.index0 | times: 2 %}
  <div class="row blogitems">
    {% for post in site.posts limit:2 offset:offset %}
        <div class="col-md-6">
          <p>{{ post.title }}</p>
        </div>
    {% endfor %}
  </div>
{% endfor %}
like image 471
T Mack Avatar asked Mar 08 '23 02:03

T Mack


1 Answers

I think you can just walk through your items while checking the modulo. Like this:

<div class="row blogitems">
{% for post in site.posts %}
  {% assign mod3 = forloop.index | modulo: 3 %}
  <div class="col-md-6"><p>{{ post.title }}</p></div>
  {% if mod3 == 0 %}<div class="col-md-6"><p>THIS IS NOT A POST</p>{% endif %}
  {% if mod3 == 0 or mod3 == 2 %}</div><div class="row blogitems">{% endif %}
{% endfor %}
</div>
like image 163
JoostS Avatar answered Mar 14 '23 21:03

JoostS