Show a list of pages with a tag

Hey! There’s a list of pages:

{% set articles = page.evaluate({'@page.children': '/articles'}) %}

I need to show a list of pages with a specific tag. How can I do that?

{% set articles = page.evaluate({'@page.children': '/articles'}) %}
{% set tags = page.evaluate({'@taxonomy': {'tag': tag}}) %}

{% set pages = articles.intersect(tags) %}

{% set taxname = uri.query('name') %}
{% set taxval = uri.query('val') %}

{% for page in pages.taxonomy.findTaxonomy({(taxname): taxval}) %}
 <li>
      <a href="{{ page.url }}">{{ page.title|e }}</a>
 </li>
{% endfor %}

It’s not working.

My decision curve

<ul>
{% for post in taxonomy.findTaxonomy({(taxname): taxval}) %}
    {% set url = post.url|split('/') %}
    {% set slug = '/' ~ url[1] %}
    {# theme_var('params.articles.root') = /articles #}
        {% if slug == theme_var('params.articles.root') %}
            <li>{{ post.title }} </li>
        {% endif %}                                        
{% endfor %}
</ul>

Can we make this easier?

@rustak, I’m not quite sure if I understand your question correctly (you have lost me in your second post), but it seems you want a collection:

  • of pages in folder ‘/articles’
  • where taxonomy.tag equals variable ‘tag’
  • and where taxonomy.{tagname} equals the value of ‘taxval’, where tagname and taxval are provided by the url.

Using the skeleton ‘Blog site’ as data and considering the following urls:

  • url: my-domain/articles?name:tag&value=photography or
  • url: my-domain/articles?name:category&value=blog

I’ve come up with the following which seems to work:

{% set tag = 'journal' %}
{% set taxname = uri.query('name') %}
{% set taxval = uri.query('val') %}

{% if taxname == 'tag' %}
	{# Merge values in array if same taxon #}
	{% set taxons = {'tag': [tag, taxval]} %}
{% else %}
	{# Use two different taxons #}
	{% set taxons = {'tag': tag, (taxname): taxval } %}
{% endif %}

{% set articles = page.evaluate({'@page.children': '/articles'}) %}
{% set categories = page.evaluate({'@taxonomy': taxons }) %}

{% set posts = articles.intersect(categories) %}

{% for post in posts %}
	<p>{{ post.title }}</p>
{% endfor %}

Note that a taxonomy filter can have multiple values which all must be true. E.g taken from the docs on Taxonomy Collections:

'@taxonomy':
    category: [blog, featured]  <- blog && featured  \
                                                      && 
    tag: [foo, bar]             <-- foo && bar       /
1 Like

Thank you, your decision is much cleaner!