Jekyll Tips - Listing Blog Posts
Here is tips to list up your Blog Posts using Jekyll.
Code which did not work
The liquid code which did not work for Project page.
<ul>
{% for post in site.posts %}
<li><a href="{{ post.url }}">{{ post.title | escape }}</a></li>
{% endfor %}
</ul>
Output:
The URL seems to point /YYYY/MM/DD/title.html
.
Code which worked
Following code worked.
The relative_url
filter shall be used,
when yor blog is located on sub-directory of the site.
<ul>
{% for post in site.posts %}
<li><a href="{{ post.url | relative_url }}">{{ post.title | escape }}</a></li>
{% endfor %}
</ul>
Output:
Better code - pagination
It is better to consider pagination.
<!-- to retrieve URL for blog posts, need to consider paginate -->
{% if site.paginate %}
{% assign posts = paginator.posts %}
{% else %}
{% assign posts = site.posts %}
{% endif %}
<!-- list blogs -->
<ul>
{% for post in posts %}
<li><a href="{{ post.url | relative_url }}">{{ post.title | escape }}</a></li>
{% endfor %}
</ul>
Output:
Reference
- jekyll/minima > home.html
- Jekyll > Liquid Filters
- How to escape liquid template tags?
- To write this article, I needed to learn how to escape liquid tags.