Posts by month in Jekyll - 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.

Related

Liquid logic to change what displays depending on time

I'm trying to figure out how to send a specific message when the time is before or after a certain time.
I have a weekly recurring event (midday every Monday) and although I can vary messaging depending on the day of the week, I can't figure out how to go more granular and send different messaging depending on whether the current time is before or after midday on a certain day.
I'm struggling to find an answer because most logic is in relation to one single event (with a date and a time) whereas mine just needs to be in relation to a specific time that repeats once a week.
This is what I started off with:
{% assign today = 'now' | date: "%A" %}
{% assign time = 'now' | time_zone: ${time_zone} %}
{% case ‘today' %}
{% when 'Monday' %}
{% if hour < 12:00%}
Today's the big day!
{% if hour > 12:00%}
You have till next Monday
{% else %}
Default copy
{% endcase %}
But I'm definitely very far off, so I would love any help!
Thank you!
You're example is almost correct. You're missing some "endif" thus I suggest you to use some liquid aware IDE. I made an example that I hope it's clear enough you can adapt to your situation.
{% assign time = 'now' | time_zone: 'UTC' %}
{% assign today = time | date: "%A" %}
{% assign hour = time | date: "%H" %}
{% assign minute = time | date: "%M" %}
{% case today %}
{% when 'Monday' %}
{% if hour < "12" %}
It's Monday Morning!
{% if hour == "10" %}
{% if minute > "20" %}
It's between 10:20 and 10:59
{% endif %}
{% endif %}
{% else %}
It's Monday Afternoon!
{% endif %}
{% when 'Tuesday' %}
It's Tuesday
{% when 'Saturday' %}
{% when 'Sunday' %}
{% if hour < "12" %}
It's the weekend in the morning!
{% endif %}
{% else %}
{% if hour < "12" %}
It's another day in the morning
{% else %}
It's another day in the afternoon
{% endif %}
{% endcase %}

Filter posts by category in Jekyll

I have a folder of markdown files each with a number of key/values. I need to filter all of the markdown files in the _faq folder by the key faq_category.
I have tried:
{% assign post = site.faqs | where: "faq_category", name-of-category %}
<ul>
<li>{{ post.title }}</li>
</ul>
However, this is showing nothing in the end.
The folder structure it should be looping through is:
jekyll
|
--faqs
|
--name-of-faq
--name-of-faq-2
Sample markdown file:
title: name of faq
faq_id: 2567
slug: title-of-faq
created: Mar 6, 2017
modified: Mar 6, 2017
faq_category: how to fly
Instead of site.faqs use site.posts to get an array of posts.
Then put the markdown files in the folder: /faqs/_posts/ for example: /faqs/_posts/faq1.md.
After that you should be able to browse them like:
{% for post in site.posts %}
{{post.title}}
{% endfor %}
To filter a specific category use: site.categories.CATEGORY or filter them like: (for example the category "mycategory")
<ul>
{% for post in site.faqs %}
{% if post.categories contains "mycategory" %}
<li>{{ post.title }}</li>
{% endif %}
{% endfor %}

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.

Archive Jekyll Posts by Year [duplicate]

This question already has answers here:
Get Jekyll Posts by Year [duplicate]
(3 answers)
Closed 8 years ago.
I'm making an "archive" if you will of all the posts on my site. I want to gather all the posts from a year. This code works fine, however; I want it to generate the <h2>2014</h2> when needed.
Basically, if year is 2014, render <h2>2014</h2> and make a <ul> of all the posts (with category of journal) from that given year.
If anyone knows of any .rb plugins that archive by year, let me know!
<h2>2014</h2>
{% for post in site.categories.journal %}
{% capture year %}{{post.date | date: "%Y"}}{% endcapture %}
{% if year == "2014" %}
<ul class="posts-in-year">
<li><p>{{ post.title }} — {{ post.date | date: "%B %d" }}</p></li>
</ul>
{% endif %}
{% endfor %}
I want it to look like this:
{% for post in site.categories.journal %}
{% capture currentyear %}{{post.date | date: "%Y"}}{% endcapture %}
{% if currentyear != year %}
<h2>{{ currentyear }}</h2>
{% capture year %}{{currentyear}}{% endcapture %}
{% endif %}
<ul class="posts-in-year">
<li><p>{{ post.title }} — {{ post.date | date: "%B %d" }}</p></li>
</ul>
{% endfor %}

Show Only Future Posts in 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.