Twig Formula: Showing X of Y, of XYZ Total Results

I wanted something I could quickly copy/paste for showing results, so I put together this Twig snippet and I thought I’d share.

When setting your per page count (16 in this example), it’ll calculate the X-Y numbers that you’re on based upon the total result count and the page that you’re on.

   {% set per_page = 16 %}
{% set current_page = query.current_page|default(1) %}
{% set total = looper.found_posts|default(0) %}

{% if total > 0 %}
  {% set start = ((current_page - 1) * per_page) + 1 %}
  {% set end = current_page * per_page %}

  {% if end > total %}
    {% set end = total %}
  {% endif %}

  {% set total_pages = (total / per_page)|round(0, 'ceil') %}
{% else %}
  {% set start = 0 %}
  {% set end = 0 %}
  {% set total_pages = 0 %}
{% endif %}

Showing {{ start }}-{{ end }} of {{ total }} Results

image

Hey @splaquet,

Thank you for sharing this. It is a useful snippet for anyone who needs a results count on a paginated Looper. A few notes for anyone who uses it:

  • Keep per_page the same as the Count set on your Looper Provider. query.posts_per_page returns the WordPress Reading setting rather than the Looper’s Count, so setting it by hand as you have done is the right approach.
  • query.current_page reads the WordPress paged value. On a static front page, WordPress uses page instead, so the numbers may stay on page 1 there.
  • If you need the page count, looper.total_pages is also available, so you do not have to calculate it.

Recommended Resources