Skip to content

Instantly share code, notes, and snippets.

@Phlow
Last active June 4, 2021 18:33
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save Phlow/a0e3fa686eb259fe7f76 to your computer and use it in GitHub Desktop.
Save Phlow/a0e3fa686eb259fe7f76 to your computer and use it in GitHub Desktop.
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 %}
@bethanyvwatson
Copy link

bethanyvwatson 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
Copy link
Author

Phlow 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
Copy link
Author

Phlow commented Feb 5, 2018

@cayirburak I corrected the code. Thankyou.

@egyjs
Copy link

egyjs 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
Copy link

geo5555 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
Copy link

BlkPingu 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