【问题标题】:How to ignore offset in Jekyll when previous post is skipped跳过上一篇文章时如何忽略 Jekyll 中的偏移
【发布时间】:2018-10-23 14:25:55
【问题描述】:

我正在尝试在 Jekyll 上创建我的第一个博客。我被困在一件事上:我的一个类别有一个部分,比如说“新闻”:

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

我的帖子

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

问题是不是我所有的帖子都会有背景拇指,我想做的就是忽略没有post.thumb的帖子。并且代码有效,但不幸的是col-md-4 块的偏移量没有忽略没有post.thumb 的帖子顺序。

下面的图片显示了我想要的:

那么我应该怎么做才能让它正常工作呢?

【问题讨论】:

    标签: jekyll liquid offset posts


    【解决方案1】:

    只需使用自定义计数器,如下所示:

    {% 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 %}
    

    【讨论】:

    • 对不起,烦人,你能解释一下你的每一步吗,如果你不介意的话,我真的是在 web dev 开始。所以我试图理解我写的所有代码,而不仅仅是复制/过去,你知道..
    • 没问题。我添加了 cmets(HTML 格式)。
    • 我发现了另一个错误,因为我的部分必须有有限的帖子列表(5),我在字符串 {% for post in site.categories.news limit:5 %} 中添加了 5 个帖子的限制,然后我创建了 6 个帖子,但是从上一个帖子(最新)开始,我删除了 thumb 以检查它将如何工作,并且 counter 完成了他的工作,但限制仍在计算第一个帖子,因为它出现了,jekyll 生成了最后的前 4 个帖子(+ 1st帖子被隐藏了)和第 6 个他没有出现在第 5 位。
    • 您没有发现错误,但您已添加功能请求。我已添加您的功能(限制为 5 个)。
    • 对不起,没听懂,你是什么意思你增加了功能(限制为5个),在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多