Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll show only specific tag on page

Tags:

ruby

jekyll

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.

like image 329
Zoey Cluff Avatar asked Nov 11 '15 09:11

Zoey Cluff


2 Answers

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.

like image 157
briantist Avatar answered Oct 15 '22 03:10

briantist


@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.

like image 2
David Jacquel Avatar answered Oct 15 '22 03:10

David Jacquel