PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to ignore offset in Jekyll when previous post is skipped

 July 08, 2022     jekyll, liquid, offset, posts     No comments   

Issue

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:

  • This is how should 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

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


Answered By - Mr. Hugo
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing