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

findTaxonomy() doesn't find pages

Started by Muut Archive 9 years ago · 3 replies · 664 views
9 years ago

Hello, in a project I'm trying to get the index of the current page in the collection of all pages with the taxonomy {'series': 'lp'}, ordered by date.

But I can't even get to that collection of pages.

TWIG
{{ var_dump(taxonomy.findTaxonomy({'series': 'lp'})) }}

Prints NULL, though the taxonomy of a particular page can be accessed by page.taxonomy.series.

My user/config/site.yaml contains taxonomies: [category, tag, series].

Any idea what could be going wrong here? Thanks!

9 years ago

May be so:

TWIG

{% for page in taxonomy.findTaxonomy({'series': 'lp'}) %}
{{ page.title }}
{% endfor%}
---
9 years ago

@stanislav's code should work. I just confirmed by testing with my own taxonomy type 'tag' and it performed as expected. I would try with the cache turned off as it could be related to that.

9 years ago

Okay, thank you for your answers!

I used the Twig tag above incorrectly, {% do var_dump() %}did print out something.

The for loop works indeed. I just hoped to create a concise macro using PHP array methods to get to the index of a certain page in an ordered, taxonomy-based Grav collection, but that didn't work out.
Now I have this solution for my problem:

TWIG
{% macro getIndex(page, series, variables) %}
  {% set i = 0 %}
  {% for colpage in variables.taxonomy.findTaxonomy({'series': 'series'}).order("date", "asc") %}
  {% set i = i + 1 %}
    {% if colpage.path == page.path %}
      {{i}}
    {% endif %}
  {% endfor %}
{% endmacro %}

_context has to be passed to variables to allow the macro to access the taxonomy variable.

It uses an ordinary counter, but it works. If you know a more elegant solution, I'd love to hear :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1338 9 years ago
Archive · by Muut Archive, 9 years ago
2 927 9 years ago
Archive · by Muut Archive, 9 years ago
2 4056 9 years ago
Archive · by Muut Archive, 9 years ago
1 2941 9 years ago
Archive · by Muut Archive, 9 years ago
3 1112 9 years ago