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.
@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.
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:
{% 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