Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Show a list of pages with a tag

Solved by pamtbaau View solution

Started by Dmitry 6 years ago · 3 replies · 809 views
6 years ago

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?

TWIG
{% 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.

6 years ago

My decision curve

TWIG
<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?

6 years ago Solution

@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:

    TWIG
    
    {% 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 %}

TXT
Note that a taxonomy filter can have multiple values which all must be true. E.g taken from the docs on [Taxonomy Collections](https://learn.getgrav.org/16/content/collections#taxonomy-collections):

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

TXT

👍 1
last edited 01/20/20 by pamtbaau
6 years ago

Thank you, your decision is much cleaner!

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 80 10 hours ago
General · by pamtbaau, 15 hours ago
1 51 15 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago