Show Only Future Posts in Jekyll - jekyll

Is there a way I can create a loop in Jekyll that shows only posts starting today and into the future in chronological order? I'm making a GitHub Pages site for a meetup group and I'm kinda stuck.

This code below can do the trick:
{% assign curDate = site.time | date: '%s' %}
{% for post in site.posts %}
{% assign postStartDate = post.date | date: '%s' %}
{% if postStartDate >= curDate %}
Post datas here
{% endif %}
{% endfor %}
But you will need to "build" your site every day as Github only updates when there is a push to your repository.

Related

Is it possible to sort a Jekyll array based on two variables?

On the frontpage of my site, I display the newest few posts:
{% for post in posts limit: 6 %}
...
{% endfor %}
This works fine.
Recently I've been going back and editing some old posts, and I want them to show in this list on the frontpage.
I don't want to change their date. Instead, I've been adding a new lastUpdated variable to each post as I edit it, with the date I edited it.
layout: example
lastUpdated: 2020-09-07
---
So now I want to modify the code on my frontpage so that it sorts by lastUpdated if a post has it, and otherwise it should sort by date.
I tried this:
{% assign posts = site.posts
| reverse
| sort: "lastUpdated"
| reverse %}
{% for post in posts limit: 6 %}
...
{% endfor %}
I thought that worked, but it turns out that it's sorting all of the edited posts (the posts that contain lastEdited variables) to the front, followed by newly created posts. I want the edited posts and the newly created posts to both show up in this list, based on their "modified" date.
I tried this:
{% assign posts = site.posts
| sort: "lastUpdated or date" %}
{% for post in posts limit: 6 %}
...
{% endfor %}
But that gives me the posts in the default order, presumably because "lastUpdated or date" is not a valid property.
I also tried this:
{% assign posts = site.posts
| sort: "lastUpdated" or "date" %}
{% for post in posts limit: 6 %}
...
{% endfor %}
But not surprisingly, this is a syntax error.
Is there a simple way to do this that I'm missing?
As soon as I wrote this, I nerd-sniped myself into cobbling together a solution that creates two arrays, and then merges them together based on the lastUpdated and date variables.
{% assign createdPosts = site.posts %}
{% assign updatedPosts = site.posts | sort: "lastUpdated" | reverse %}
{% assign createdIndex = 0 %}
{% assign updatedIndex = 0 %}
{% for i in (1..10) %}
{% assign createdDate = createdPosts[createdIndex].date | date: "%Y-%m-%d" %}
{% assign updatedDate = updatedPosts[updatedIndex].lastUpdated | date: "%Y-%m-%d" %}
{% if createdDate > updatedDate or updatedDate == nil %}
// HTML for createdPosts[createdIndex]
{% assign createdIndex = createdIndex | plus:1 %}
{% else %}
// HTML for updatedPosts[updatedIndex]
{% assign updatedIndex = updatedIndex | plus:1 %}
{% endif %}
{% endfor %}
One thing to note is that I couldn't compare date and lastUpdated directly, because I got this error: comparison of Date with Time failed, so I had to convert them both to strings.
This feels pretty complicated, so I might end up going back and populating every post with lastUpdated to keep it simple.

Paginate highly filtered Jekyll collection

I’m building an ICO Website. The website contains a ‘coins’ collection, this collection contains all the ico coin pages. I have then created pages such as https://moonlandingnetwork.netlify.com/upcomingico , https://moonlandingnetwork.netlify.com/activeico etc to display these coins based on whether they are upcoming, active or ended. I used this code to filter these coins based on the date and based on whether the coin is featured.
{% assign featuredcoins = site.coins | where: ‘ico.featured’, ‘true’ %}
{% assign regularcoins = site.coins | where: ‘ico.featured’, ‘false’ %}
{% assign timeframe = 86400 %}
{% assign current_date = “now” | date: “%s” | minus: timeframe %}
{% assign featuredUpcoming = “” | split: ‘,’ %}
{% for coin in featuredcoins %}
{% assign pre_ico_end_date = coin.pre-ico.end-date | date: “%s” | plus: 0 %}
{% assign ico_start_date = coin.ico-detail.start-date | date: “%s” | plus: 0 %}
{% if current_date > pre_ico_end_date and ico_start_date > current_date %}
{% assign featuredUpcoming = featuredUpcoming | push: coin %}
{% endif %}
{% endfor %}
{% assign regularUpcoming = “” | split: ‘,’ %}
{% for coin in regularcoins %}
{% assign pre_ico_end_date = coin.pre-ico.end-date | date: “%s” | plus: 0 %}
{% assign ico_start_date = coin.ico-detail.start-date | date: “%s” | plus: 0 %}
{% if current_date > pre_ico_end_date and ico_start_date > current_date %}
{% assign regularUpcoming = regularUpcoming | push: coin %}
{% endif %}
{% endfor %}
{% for coin in featuredUpcoming limit: 2 %}
<li>Featured Coin</li>
{% endfor %}
{% for coin in regularUpcoming %}
<li>Regular Coin</li>
{% endfor %}
Where I’m stuck is I need to paginate this. I know jekyll doesn’t support pagination of collections. I came across Jekyll paginate V2, Octopress Paginate and this code https://gist.github.com/Phlow/5613fb3f18946f577f071e2a258749a3 in my research. But i couldn’t get any of those to work.
I also require the pagination to work in such a way that the featured coins change on each new page along with the regular coins. Can somebody help me direct on what i should do.

Find post where key has specific value in Jekyll

I need to be able to compare a value in each of the markdown files in a folder called in_media to the user_id of the current page stored in a _users folder and only display the post title that have that value from the in_media folder.
User markdown file in _users folder
---
user_id: 123
title: bob
---
Post markdown from in_media folder
---
users: 123
---
I tried the following:
{% for this_user in site.in_media %}
{% for user in page.user %}
{% if this_user == user.user_id %}
<li><a href="{{ post.external_link }}">{{ post.title }}</a </li>
{% endif %}
{% endfor %}
{% endfor %}
However, this is not returning anything
Try this way:
{% for post in site.in_media %}
{% if post.value == page.title %}
<li>{{ post.title }}</li>
{% endfor %}
{% endfor %}
I'm not sure that you can use external_link, never heard of it. Maybe you'll need to build permalink manually - depending on your _config.xml.
Also note the collection should be properly set up to work with permalinks.

Comparing dates in Jekyll

I'm trying to compare the current date with the date of the post in jekyll/liquid. If the current date is less than the date of the post, then I want to show the post title.
Here is my code so far:
<header class="announce-ticker">
<div class="container">
{% capture currentDate %}
{{ 'now' | date: '%s'}}
{% endcapture %}
{% assign eventCount = 0 %}
{% assign eventPosts = site.posts %}
{% for post in eventPosts %}
{% capture postDate %}
{{ post.date | date: '%s'}}
{% endcapture %}
{% if currentDate < postDate %}
{% post.title %}
{% assign eventCount = 1 %}
{% endif %}
{{ currentDate}}
{{ postDate }}
{% endfor %}
{% if eventCount == 0 %}
<p>No events</p>
{% endif %}
</div>
</header>
My problem is that it's not showing the post when it is greater than current date.
Any help would be appreciated. Thanks!
Future dated posts are not published by default. Basic way to enable them through command line is to use the future option:
jekyll serve --future
Alternatively, you can add the future parameter to your _config.yml:
future: true
More options in this article.

Posts by month in Jekyll

I am trying to create an archive page of posts from my website. What I would like is to be able to have a pages for each list of posts by month in this format:
www.mywebsite.com/2016/11 would display all posts for November 2016.
Can I have a page for each month I have posted that is dynamically created each time I post in a new month? I don't want to have to manually create a new page for each month.
I already can group posts by year like so:
<ul>
{% for post in site.posts %}
{% assign currentdate = post.date | date: "%Y" %}
{% if currentdate != date %}
<li id="y{{currentdate}}">{{ currentdate }}</li>
{% assign date = currentdate %}
{% endif %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
Thanks for any help.
You can modify your date filter to integrate the month, e.g. date: "%B %Y". That's what I used in a layout, with a separate <ul> for each month.
From documentation, month-only values for the date filter are:
%b: abbreviated month name.
%B: full month name.
%m: month of the year (01 - 12).
Complete loop:
<ul>
{% for post in site.posts %}
{% assign currentdate = post.date | date: "%B %Y" %}
{% if currentdate != date %}
<li id="y{{currentdate}}">{{ currentdate }}</li>
{% assign date = currentdate %}
{% endif %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
About generating the pages, AFAIK this can only be done through a plugin. If you can't use plugins, e.g. if you're hosting your pages on GitHub, the best you can do is to reduce your pages to a short YAML frontmatter relying on a layout, like in this answer.