11ty collections tag links

More of a reminder for myself next time Image need to do this!

Adding a collection tag to a post as a link

There are some lovely ways to automate sets of tags with a list page. Hoever I don't think these can have additional content to make them into a proper topic hub page so I am building them out manually.

For posts in my Articles and Shorts sections I have started adding tags to the modelling topic ones. After the author sign off with date, and update date if there is one, I have added links to the tag hub page.

I started with this 11ty code:

{% if tag == "Modelling" %}| <a href="/narrow-gauge-modelling">Modelling</a>{% endif %}
{% if tag == "Loxley" %}| <a href="/loxleybarton">Loxley Barton Falls</a>{% endif %} 

But it wasn't showing up the tag links. Turns out that because I have multiple tags on some in this form:

tags:
- Loxley
- Modelling

Turns out that 11ty concatentates them as a string, of course, so I need to cycle through that and pull out the tags that I need.

This gave me:

{% for tag in tags %} 
  {% if tag == "Modelling" %}| <a href="/narrow-gauge-modelling">Modelling</a>{% endif %}
  {% if tag == "Loxley" %}| <a href="/loxleybarton">Loxley Barton Falls</a>{% endif %}
{%- endfor %}

Which works perfectly. I am sure there is a more robust method but for now, for me, this is working great!

By Simon Cox Published: Sun, Mar 17, 2024

Next short: The cobbles of Mousehole

Previous short: Tracking 404 broken links with Fathom Analytics

Latest Shorts

Or all the short articles

Latest articles

Or all the articles