Removing specific posts in for loop

howdy folks

first time here, but long time Jekyll user [still on version 3.8]

there is this problem I’ve been meaning to fix or solve, searched for a long time & still have not found anything. I feel I’m close, but my tests keep me at a distance to get the answer

so I have a for loop that shows all my posts, great, that works fine.
but I have a bigger post at the top, another for loop going thru the same posts, but limiting it to 1. this is basically the newest post people come to, again, works fine

the only problem is with the other posts. I can’t seem to remove the [newest] post that is being used as the newest post for the top, the one thats larger

I have tried offset, with both positive number & negative [just to test if it did work, it didn’t] as well as sort & the such, plus reverse etc. these do work to a point, but I cant seem to remove the newest post from the for loop

this is something of what ive worked with. though have put back things just for the sake of showing what I have been trying

{% assign sorted_music = site.music | sort:'order' %}
{% for page in sorted_music reversed %}
// stuff in here
{% endfor %}

these are dated & are sorted with them. but getting that first post removed is hard. I can do the last easily with offset, but the other way around is a bit of a head scratch & not seen anyone else ask it, from what I have seen

would be great if someone could help, or point me to somewhere that has the answer

thanks again

x
Lew

offset should be the correct tool for the job. If it isn’t working, I’d need to see the code to help. Do you have this in a public repo?


What about adding something to the frontmatter of the post you want in the first loop? Something like:

---
first_loop: true
---

Then you could check for it like this:

{% assign first_post = site.posts | where: 'first_loop', true %}
{% for post in first_post %}
  {{ post.content }}
{% endfor %}

This wouldn’t be ideal if that first post changes. You’d need to go back to update the frontmatter in the old post every time … but it’d probably work.

1 Like

hi @brad

thanks for the help

aye, offset is weird for me, or seems to act weird. maybe its the version of Jekyll im using 3.8, or just that its always like this

but this is the code I have currently. its my ‘throwing against the wall’ code for testing. ill take something away, test, then try something else

{% assign sorted_writing = site.writing | sort:'date' %}
{% for page in site.writing reversed offset:1 %}
/// here lays the basic insides of what post list. such as title etc. nothing more
{% endfor %}

this kind of works. but no matter what, offset doesn’t take the first off, since im using reverse. but I need to use reverse, so the latest post is at the top, not the bottom

I guess to work out a way to put the latest at the top, without a reverse could work

ill have more of a play around & try things. may have to think about how they are presented. so not using date, but something else, maybe

thank again, ill dig further to work something out

Is reversed working for you? The syntax in the docs is reverse: reverse – Liquid template language. Although I see in this CloudCannon tutorial, they use reversed also.

I wonder if moving the reverse call to the assignment would make a difference? That way the only thing in the forloop would be the offset. Perhaps try adding a pipe between the for and the offset filter?

{% assign sorted_writing = site.writing | sort: 'date' | reverse %}
{% for page in site.writing | offset: 1 %}
  // here lays the basic insides of what post list. such as title etc. nothing more
{% endfor %}

I can’t find anything that says what version of Liquid the reversed or offset filters were added …but they’ve been around for a while. It shouldn’t be your Gem versions that are causing the problem.

If the only problem is the offset, you could skip it with a conditional in your forloop. Something like this:

{% assign sorted_writing = site.writing | sort: 'date' | reverse %}
{% for page in site.writing %}
  {% unless forloop.first %}
    // here lays the basic insides of what post list. such as title etc. nothing more
  {% endunless %}
{% endfor %}
1 Like

well you are my Jekyll saviour

I dont now why it should be different in the assign than from the for. but that seemed to have dislodged something from the back of this web designs throat & now things are as they should be

ill do a little bit o’ reading as to why things are better or whatever in assign. but I can say your help has… errr… helped :wink:

x
thanks again