Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List tags within a specific category in Jekyll

Tags:

jekyll

liquid

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:

  • use category to distinguish two types of posts (e.g., blog and portfolio)
  • use tag as normal

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.

like image 272
dirkchen Avatar asked May 07 '14 15:05

dirkchen


Video Answer


1 Answers

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.

like image 149
Ron Avatar answered Oct 25 '22 04:10

Ron