Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jekyll: List all categories with according post count and show and link all post items listed in the according category
<h2>Categories</h2>
<ul>
{% assign categories_list = site.categories %}
{% if categories_list.first[0] == null %}
{% for category in categories_list %}
<li><a href="#{{ category | downcase | downcase | url_escape | strip | replace: ' ', '-' }}">{{ category | camelcase }} ({{ site.tags[category].size }})</a></li>
{% endfor %}
{% else %}
{% for category in categories_list %}
<li><a href="#{{ category[0] | downcase | url_escape | strip | replace: ' ', '-' }}">{{ category[0] | camelcase }} ({{ category[1].size }})</a></li>
{% endfor %}
{% endif %}
{% assign categories_list = nil %}
</ul>
{% for category in site.categories %}
<h3 id="{{ category[0] | downcase | url_escape | strip | replace: ' ', '-' }}">{{ category[0] | camelcase }}</h3>
<ul>
{% assign pages_list = category[1] %}
{% for post in pages_list %}
{% if post.title != null %}
{% if group == null or group == post.group %}
<li><a href="{{ site.url }}{{ post.url }}">{{ post.title }} <time datetime="{{ post.date | date_to_xmlschema }}" itemprop="datePublished">{{ post.date | date: "%B %d, %Y" }}</time></a></li>
{% endif %}
{% endif %}
{% endfor %}
{% assign pages_list = nil %}
{% assign group = nil %}
</ul>
{% endfor %}
@konstantin-morenko

This comment has been minimized.

Copy link

commented Jan 8, 2017

You've forgot about closing </span> in line 23.
</time></span></a>
Full version here https://gist.github.com/konstantin-morenko/1c301ad5a6cca6cded99e6726cf2f71a

@cayirburak

This comment has been minimized.

Copy link

commented Sep 12, 2017

Thanks , but I cant slide categorie posts when I click a categorie.

@bethanyvwatson

This comment has been minimized.

Copy link

commented Oct 4, 2017

On line 10, should "#{{ tag[0] }}" be #{{ category[0] }}?

Anyway, I got the top snippet working for me that way. Thank you for this!

@Phlow

This comment has been minimized.

Copy link
Owner Author

commented Feb 5, 2018

Hey @cayirburak, I just saw your comment and you're right. I deleted the <span>

Hey @bethanyvwatson – my code is confusing, but correct. The tag is just a placeholder from the loop {% for tag in site.categories %} – But I changed it to category to get rid of the unnecessary confusing. Thankyou for pointing it out.

@Phlow

This comment has been minimized.

Copy link
Owner Author

commented Feb 5, 2018

@cayirburak I corrected the code. Thankyou.

@el3zahaby

This comment has been minimized.

Copy link

commented Feb 16, 2018

How can I create a page for each category automatically in Jekyll ?

example:

the url of the post : https://UsrNm.github.io/Category1/Category2/2018/02/16/Post1.html

I want when I go to link like: https://UsrNm.github.io/Category1/Category2/

Get All posts in category Category1 and Category2

or when I go to link like: https://UsrNm.github.io/Category2/

Get All posts in category Category2 only

@geo5555

This comment has been minimized.

Copy link

commented Mar 29, 2018

when i click the category, my posts don't show up. do I have to set the permalink and create a page?
because now it just points to category/year/month/day/category

@BlkPingu

This comment has been minimized.

Copy link

commented Jul 16, 2018

Let's say I don't use a default front-matter variable like tags or categories, but instead a non-default one like author / authors in each post? I does work for categories, I have these too, but I want it for authors too.
If i replace site.categories with site.authors I get a Liquid Exception: 0 is not a symbol nor a string in xxx.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.