Generating List of Posts
This post shows how to use jekyll to generate list of all posts in a website. I am using liquid templating engine to generate list of posts here.
PRINT LATEST 3 POSTS EXCLUDING TOP-MOST 2:
To print latest 3 posts excluding top-most 2 posts will require {% for ipost in postlistsortedbydate limit:3 offset:2 %}
inside for loop.
CODE:
{% assign postlistsortedbydate = site.posts %}
<p><ul>
{% for ipost in postlistsortedbydate limit:3 offset:2 %}
<li>
<a href="{{ site.baseurl }}{{ ipost.url }}">{{ ipost.title }} - {{ ipost.date | date_to_rfc822 }}</a>
</li>
{% endfor %}
</ul></p>
OUTPUT:
- Installing & Handling Python Distributions & Jupyter Notebooks & Deep Learning - Wed, 17 May 2017 20:35:43 +0530
- Installing Apache Spark Standalone-Cluster in Windows - Wed, 17 May 2017 13:36:21 +0530
- Installing Hadoop Suite As Standalone Cluster In Windows - Wed, 17 May 2017 13:04:26 +0530
PRINT POST INDEX ALONG WITH LATEST 3 POST
To print post index along with latest 3 posts with you’ve to calculate index yourself as {% assign postindex = postindex | plus: 1 %}
.
CODE:
{% assign postindex = 0 %}
<p><ul>
{% for ipost in postlistsortedbydate limit:3 offset:0 %}
{% assign postindex = postindex | plus: 1 %}
<li>
<a href="{{ site.baseurl }}{{ ipost.url }}">{{ postindex }}# {{ ipost.title }}</a>
</li>
{% endfor %}
</ul></p>
OUTPUT:
- 1# RancherOS Getting Started Guide
- 2# Cloud Platform Providers
- 3# Installing & Handling Python Distributions & Jupyter Notebooks & Deep Learning
PRINT TOP 3 POST SORTED BY TITLE
To print latest 3 posts sorted by title first you have to sort array of posts by title into another variable as {% assign postlistsortedbytitle = site.posts | sort: 'title' %}
.
CODE:
{% assign postlistsortedbytitle = site.posts | sort: 'title' %}
<p><ul>
{% for ipost in postlistsortedbytitle limit:3 offset:0 %}
<li>
<a href="{{ site.baseurl }}{{ ipost.url }}">{{ ipost.title }} - {{ ipost.date | date_to_rfc822 }}</a>
</li>
{% endfor %}
</ul></p>
OUTPUT:
- About MQTT Servers & Brokers - Fri, 07 Oct 2016 11:00:00 +0530
- About Markdown Files And Editors - Wed, 28 Sep 2016 11:00:00 +0530
- BigDATA: Apache Spark Introduction - Tue, 25 Oct 2016 12:00:00 +0530
PRINT OLDEST 5 POST, OFFSET 1
To print oldest 5 posts offet 1 if we can’t use: {% for ipost in postlistreversed reversed limit:3 offset:1 %}
with reversed keyword. Instead you have to generate another list by reversign and assigning first as {% assign postlistreversed = site.posts | reverse %}
and then apply limit:3 offset:1
filter on for loop of new variable named postlistreversed
CODE:
{% assign postlistreversed = site.posts | reverse %}
<p><ul>
{% for ipost in postlistreversed limit:5 offset:1 %}
<li>
<a href="{{ site.baseurl }}{{ ipost.url }}">{{ ipost.title }} - {{ ipost.date | date_to_rfc822 }}</a>
</li>
{% endfor %}
</ul></p>
OUTPUT:
- Jekyll Data Collections - Tue, 27 Sep 2016 12:47:00 +0530
- Generating List of Posts - Tue, 27 Sep 2016 12:47:00 +0530
- Generating List of Pages - Tue, 27 Sep 2016 12:47:01 +0530
- Templating Functionality in Jekyll!! - Tue, 27 Sep 2016 12:47:10 +0530
- Using GitHub with Tortoise GIT - Tue, 27 Sep 2016 13:59:00 +0530