Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit jekyll post list to certain categories

Tags:

jekyll

liquid

I have a jekyll website and I have a category (called photo) and I wanted to create a separate layout for a page that would list just the posts that were in the photo category. I also want to keep posts with the photo category out of the main index page.

like image 668
Mark Szymanski Avatar asked Jan 23 '11 19:01

Mark Szymanski


3 Answers

All categories are available within the site object, access the posts of a category via site.categories.photo so your loop would look like this

{% for post in site.categories.photo %}
    # render the photo post html
{% endfor %}
like image 137
David Burrows Avatar answered Oct 18 '22 01:10

David Burrows


I just used an {% unless %} block in the main index page to make sure the post wasn't a photo. Example:

{% unless post.category == "photo"%}
    {% comment %} List posts... {% endcomment %}
{% endunless %}

And I used the same thing for showing only photos. Just with an if instead of unless.

like image 24
Mark Szymanski Avatar answered Oct 18 '22 01:10

Mark Szymanski


The category is case sensitive as well. If your category is photo then it will look like this:

{% for post in site.categories.photo %}
    # render the photo post html
{% endfor %}

If your category is Photo then it will look like this:

{% for post in site.categories.Photo %}
    # render the photo post html
{% endfor %}

Just a quick detail that I tripped up on my build so I thought I would share.

like image 4
Trevor Glass Avatar answered Oct 18 '22 01:10

Trevor Glass