Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll blog show posts under a category

I want to show posts that are from a certain category. For example, going to url http://example.com/posts/programming will list all the posts that have "programming" as their category.

My general blog index looks like below

  {% for post in site.posts %}
    <div class="post-box">
      <div class="post-title">
        <a href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
      </div>
      <span class="post-date">{{ post.date | date: "%b %-d, %Y" }}</span>
      <p class="post-excerpt">{{ post.excerpt }}</p>
      <div>
        {% for category in post.categories %}
          <a href="#">#{{ category }}</a>
        {% endfor %}
      </div>
    </div>
  {% endfor %}

If Jekyll does not automatically provide a specific url for each category, I would have to dynamically change the available posts based on the given url. I can of course create a directory dedicated to each category and then make index.html inside it, but there must be a better way.

It would be perfect if there were a way to dynamically change {% for post in site.posts %} part to {% for post in posts in some_category %} with javascript. Any help would be wonderful.

like image 406
Maximus S Avatar asked Jan 25 '15 22:01

Maximus S


People also ask

What are categories in a blog?

A blog category is a topic you address on your blog. Your category list is like the table of contents for your blog. Categories are broad and can encompass smaller, more defined topics (i.e., tags). A category title should be descriptive and can be several words long.


1 Answers

I can of course create a directory dedicated to each category and then make index.html inside it, but there must be a better way.

This is a great way to do it, isn't much work at all and works flawlessly on gh-pages. It's exactly what I do on my own sites as I prefer to keep my .md posts filed by category in the directory structure, so I simply have:

/blog/
    /_posts/20015-01-01-my-awesome-post.md
    index.html

/labs/
    /_posts/20015-01-01-my-technical-post.md
    index.html

I find it preferable for maintenance to not have 1001 posts all in _posts/ and I get the pretty permalink structure I want without entering categories in each posts front-matter.

like image 85
Seth Warburton Avatar answered Nov 14 '22 23:11

Seth Warburton