scratching my head over this - help much appreciated.
I want to display a list of all my Jekyll posts, organised by category. I know Line 3 isn't correct but I can't figure out what it should be. Any ideas? Thanks!
{% for category in site.categories %}
<h3>{{ category | first }}</h3>
{% for post in page.categories.category %}
{{ post.title }}<br>
{% endfor %}
{% endfor %}
Got it! Needed an intermediate posts loop before listing out individual posts
<ul>
{% for category in site.categories %}
<li><a name="{{ category | first }}">{{ category | first }}</a>
<ul>
{% for post in category.last %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
fyi, if anyone wants to just list the posts in one category, this works (differs from above example as the category returns a list of posts...
<p>Posts in category "basic" are:</p>
<ul>
{% for post in site.categories.basic %}
{% if post.url %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endif %}
{% endfor %}
</ul>
Now there is an official plugin available for this. jekyll-archives
.
In order to make use of it,
Add jekyll-archives
to your Gemfile
and _config.yml
file.
add a configuration something similar to the below as per your need.
jekyll-archives:
enabled: all
layouts:
year: archive/year
month: archive/month
day: archive/day
tag: archive/tag
category: archive/category
permalinks:
year: '/:year/'
month: '/:year/:month/'
day: '/:year/:month/:day/'
tag: '/tags/:name/'
category: '/category/:name/'
The layouts
can make use of the following page attributes depending on the archive type
.
year
, month
, day
, tag
, category
)Nil
otherwise.)page.type
you should parse out the date and month field )Here is a sample layout for archive based on years
<h1>Archive of posts from {{ page.date | date: "%Y" }}</h1>
<ul class="posts">
{% for post in page.posts %}
<li>
<span class="post-date">{{ post.date | date: "%b %-d, %Y" }}</span>
<a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
<h5>Categories</h5>
{% for category in site.categories %}
{% assign cat = category[0] %}
<h6><a href="#">{{ cat }}</a></h6>
{% for post in site.categories[cat] %}
<a href="{{ post.url }}">{{ post.title }}</a> <small>{{ post.date }}</small>
{% endfor %}
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With