Jekyll循环中的每第4个项目显示不同

时间:2017-09-12 06:55:41

标签: html loops for-loop jekyll liquid

我正在使用jekyll通过博客文章填充我的博客页面。我希望每个div使用"行"类,但我也希望每个第4项都是一个广告(跳过帖子并移到下一行但仍然包含其他不是帖子的内容)。

因此,如果有6个帖子,则输出应该如下所示

<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 -->

我有post loop部分,但我无法弄清楚如何添加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 %}

1 个答案:

答案 0 :(得分:2)

我认为您可以在查看modulo时浏览您的商品。像这样:

<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>