0

is there any way to add ascending numbers to entries archive, i.e. Entry #1 of 100, Entry #2 of 100, #3 of 100 etc. Thanks in advance.

2 Answers 2

1

Since you're using pagination, you can calculate the offset using the current page and number of items per page.

{% set limit = 10 %}
{% set archiveQuery = craft.entries()
    .section('blog')
    .limit(limit)
%}

{% paginate archiveQuery as pageInfo, posts %}

{% set offset = (pageInfo.currentPage - 1) * limit %}

{% for post in posts %}
    Entry {{ offset + loop.index }} of {{ pageInfo.total }}
{% endfor %}
1
1

I’m not sure if this is what you’re asking for, but you can number fetched entries inside a for loop in Twig using the current iteration of the loop (loop.index).

{% set entries = craft.entries.all() %}
{% for entry in entries %}
    Entry #{{ loop.index }} of {{ entries|length }}
{% endfor %}

To make this work when paginating entries, you can add the current page’s first entry number (pageInfo.first) to the zero-indexed iteration of the loop (loop.index0).

{% set entryQuery = craft.entries.limit(10) %}
{% paginate entryQuery as pageInfo, entries %}
{% for entry in entries %}
    Entry #{{ pageInfo.first + loop.index0 }} of {{ pageInfo.total }}
{% endfor %}

References:
https://craftcms.com/docs/5.x/development/element-queries.html#navigating-pages
https://twig.symfony.com/doc/3.x/tags/for.html#the-loop-variable

3
  • ok, thx, will try and let you know Commented Jul 8 at 10:07
  • It works, but, for pagination state, for every next page, it starts from 1 of total, rather than continuing from the previous page. Commented Jul 8 at 19:48
  • You didn’t mention that in your question, but I’ve added a second code sample to my answer for the case when you’re paginating entries.
    – Ben Croker
    Commented 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.