问题
i'm trying to create my first blog on jekyll. And i stucked in one stupid thing. so the theme is next: i have a section for one of my categories, let it be "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: discription
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"
---
so the problem is that not all of my posts will have background thumb, and all i want to do is to ignore the posts wich 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.
in picture bellow ill try to explain what i want:
This is how schould be, if all my posts have post.thumb(bg_image)
This is how should be, if my post Item2 has no post.thumb(bg_image), it just not showing up in section
And this is how my code works :D
so what a should do to make it works right?
P.S. My english is bad enough, so im sorry for grammar mistakes and i`m assking u guys to give answers as much simpple as u can do...
回答1:
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 %}
来源:https://stackoverflow.com/questions/52951502/how-to-ignore-offset-in-jekyll-when-previous-post-is-skipped