Is it possible to access tags from a different type of page

On my site I have two different types of content: “posts” which are the native Jekyll posts in the default _posts folder, and “projects” which are a collection located in a custom _projects folder. I’m trying to build a tags page that incorporates tags from both pages. Jekyll’s site.tags variable accesses tags in the _posts folder, but I want to access tags in the _projects folder too – how would I approach this? I’m currently trying to capture tags for both content types like so:

{% capture tags %}
  {% for project in site.projects %}
    {% for tag in project.tags %}
      {{ tag }}
    {% endfor %}
  {% endfor %}
  {% for post in site.posts %}
    {% for tag in post.tags %}
      {{ tag }}
    {% endfor %}
  {% endfor %}
{% endcapture %}

Then I’m trying to convert to an array and iterate over it to display the tags:

{% assign all_tags = tags | split: " " | uniq | sort %}

{% for tag in all_tags %}
<h3 id="{{ tag }}">{{ tag }}</h3>
{% endfor %}}

This works OK, but I also want to display a link to each post/project with the relevant tag. I can do this for posts by using something like {% for post in site.tags[tag] %}, but I can’t work out how to do this simultaneously for both “posts” and “projects” at the same time.

Has anyone come across an issue like this before?

Many thanks.

Maybe this recent q and answer helps

{%- assign product_tags = site.products | map: 'tags' %}

See more here

1 Like