dirkchen
dirkchen

Reputation: 195

List tags within a specific category in Jekyll

I am migrating my Wordpress blog to Jekyll, which I like a lot so far. The current setup in the new site is like this:

The challenge right now is to display all tags within a category because I want to create two separate tag clouds for two types of posts.

As far as I know, Liquid supports looping over all tags in a site like this:

{% for tag in site.tags %}
    {{ tag | first }}
{% endfor %}

But I want to limit the scope to a specific category and am wishing to do something like this:

{% for tag in site['category'].tags %}
    {{ tag | first }}
{% endfor %}

Any advice will be appreciated.

Upvotes: 7

Views: 1430

Answers (2)

João
João

Reputation: 121

This will work, it will list only the tags on post of category 'X'. Replace X with the name of the category.

{% for post in site.categories['X'] %}
{% for tag in post.tags %}

   {{ tag  }}

{% endfor %}
{% endfor %}

Upvotes: 0

Ron
Ron

Reputation: 1161

This seems to work for all kinds of filters like category or other front matter variables - like "type" so I can have type: article or type: video and this seems to get tags from just one of them if I put that in the 'where' part.

{% assign sorted_tags = site.tags | sort %}
{% for tag in sorted_tags %}
{% assign zz = tag[1] | where: "category", "Photoshop" | sort %}
{% if zz != empty %}

<li><span class="tag">{{ tag[0] }}</span>
<ul>
  {% for p in zz %}
  <li><a href="{{ p.url }}">{{ p.title }}</a></li>
  {% endfor %}
 </ul>
 </li>
 {% endif %}

 {% endfor %}

zz is just something to use to filter above the first tag[0] since all it seems to have is the tag itself, so you can filter anything else with it. tag[1] has all of the other stuff.

Initially I was using if zz != null or if zz != "" but neither of them worked.

Upvotes: 9

Related Questions