57

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 %}
Alex G
  • 1,646
  • 2
  • 17
  • 19

6 Answers6

75

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>
David Jacquel
  • 51,670
  • 6
  • 121
  • 147
Alex G
  • 1,646
  • 2
  • 17
  • 19
  • 24
    I have test in my local and found the first of {{ posts }} is the category name and will be a empty line in html, so I add {% if post.url %} ahead `
  • {{ post.title }}
  • ` to remove category line – Tanky Woo Mar 11 '14 at 05:49
  • hey, thanks for this, it works great! I have a question though (sorry, total jekyll noob) and probably would help improving your answer: do you know (if possible) how to put this code in a page that sits in /category/, reads the from the url and pulls the list of posts accordingly? (edit) my question dups here: http://stackoverflow.com/questions/25958652/how-to-create-a-link-to-each-category/25979472#25979472 – gru May 03 '15 at 18:08