I'm using jekyll, and I'm trying to have a page ( /play) show only posts tagged Play, and the same with Work (/work). How can I go about doing this?
Thanks.
You can do this by iterating through the site's tags and indexing into the array with the tag name, similar to the following:
{% for post in site.tags.Play %}
<h2>{{ post.title }}</h2>
<time>{{ post.date }}</time>
{% endfor %}
It's a bit confusing because it seems like site.tags
returns a collection of tags, but it actually contains the full posts, indexed by tag.
See site.tags.TAG
in the Jekyll Variables page.
@briantist is right, you have to dig in the site.tags collection.
In order get a more maintainable code, you can use Jekyll includes.
A tag page (eg: ruby.html) is just calling an include passing it the ruby tagName :
---
layout: page
title: Ruby
---
{% include tagPagesLoop.html tagName='ruby' %}
_includes/tagPagesLoop.html
<h2>All {{ include.tagName }} posts</h2>
{% for post in site.tags[include.tagName] %}
<h3>{{ post.title }}</h3>
other stuff here
{% endfor %}
All changes made in this include is present in all pages that use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With