<p>As I already wrote in my comment 3 days ago something like this should work</p>

<pre><code>{% paginate craft.entries.section('mySection').limit(10) as pageInfo, pageEntries %}

{% for page, url in pageInfo.getPrevUrls(3) %}
    &lt;a href="{{ url }}"&gt;{{ page }}&lt;/a&gt;
{% endfor %}

&lt;span class="current"&gt;{{ pageInfo.currentPage }}&lt;/span&gt;

{% for page, url in pageInfo.getNextUrls(3) %}
    &lt;a href="{{ url }}"&gt;{{ page }}&lt;/a&gt;
{% endfor %}

{% if pageInfo.totalPages - pageInfo.currentPage &gt; 3 %}
    &lt;span&gt;...&lt;/span&gt;

    &lt;a href="{{ pageInfo.lastUrl }}"&gt;{{pageInfo.totalPages}}&lt;/a&gt;
{% endif %}
</code></pre>

<p><strong>[EDIT]:</strong> In the above script you will come across situations where the gap sometimes occurs between consecutive numbers, which can look silly.</p>

<p><a href="https://i.sstatic.net/E8it6.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/E8it6.png" alt="Gap between consecutive numbers"></a></p>

<p><a href="https://i.sstatic.net/2BIsM.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/2BIsM.png" alt="Single page gap"></a></p>

<p>To fix this you can add a little extra logic to it:</p>

<pre><code>{% set amountVisible = amountVisible|default(2) %}
{% if pageInfo.currentPage &gt; amountVisible + 1 %}
    &lt;a href="{{ pageInfo.firstUrl }}"&gt;1&lt;/a&gt;

    {% if pageInfo.currentPage == amountVisible + 3 %}
        &lt;a href="{{ pageInfo.getPageUrl(2) }}"&gt;2&lt;/a&gt;
    {% elseif pageInfo.currentPage &gt; amountVisible + 3 %}
        &lt;span&gt;...&lt;/span&gt;
    {% endif %}
{% endif %}

{% for page, url in pageInfo.getPrevUrls(amountVisible) %}
    &lt;a href="{{ url }}"&gt;{{ page }}&lt;/a&gt;
{% endfor %}

&lt;span class="current"&gt;{{ pageInfo.currentPage }}&lt;/span&gt;

{% for page, url in pageInfo.getNextUrls(amountVisible) %}
    &lt;a href="{{ url }}"&gt;{{ page }}&lt;/a&gt;
{% endfor %}

{% if pageInfo.totalPages - pageInfo.currentPage &gt; amountVisible %}
    {% if pageInfo.totalPages - pageInfo.currentPage == amountVisible + 2 %}
        &lt;a href="{{ pageInfo.getPageUrl(pageInfo.totalPages - 1) }}"&gt;{{pageInfo.totalPages - 1}}&lt;/a&gt;
    {% elseif pageInfo.totalPages - pageInfo.currentPage &gt; amountVisible + 1 %}
        &lt;span&gt;...&lt;/span&gt;
    {% endif %}

    &lt;a href="{{ pageInfo.lastUrl }}"&gt;{{pageInfo.totalPages}}&lt;/a&gt;
{% endif %}
</code></pre>

<p>Now this added logic will do two things:</p>

<ol>
<li>If there is no gap, it won't render a thing.</li>
<li>If the gap only amounts to one page it will render a page number instead.</li>
</ol>

<p>This results in the following:</p>

<p><a href="https://i.sstatic.net/coymK.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/coymK.png" alt="No more &quot;useless&quot; gaps"></a></p>

<p>As an added bonus: if you add the <code>amountVisible</code> variable on top with a default value you can put this in a twig partial which you can then easily implement on the paged view like so:</p>

<p><code>{% include 'partials/pager' with { pageInfo: pageInfo, amountVisible: 3 } %}</code></p>

<p>(Note that passing <code>pageInfo</code> explicitly is not required, but I do it for the sake of completeness and to prevent accidental cleanups of "unused" vars.)</p>

CC BY-SA 4.0
2017-11-27T19:20:52.733Z
2019-04-30T14:06:58.743Z
2019-04-30T14:06:58.743Z
Robin Schambach
<p>I am working on a pagination for some articles. I am using a <code>for</code> loop to get the 3 pages after and before the current page <code>.getPrevUrls(3)</code> and <code>.getNextUrls(3)</code>
So far it looks like this, where 35 is the last page and the black one is the current page.</p>

<p><a href="https://i.sstatic.net/0NL4R.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/0NL4R.jpg" alt="enter image description here"></a></p>

<p>The problem I'm trying to fix is when the current page is within the 3 page range <code>.getNextUrls(3)</code> of the last page.</p>

<p><a href="https://i.sstatic.net/vMsKY.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vMsKY.jpg" alt="enter image description here"></a></p>

<p>How can I create an <code>if statement</code> to hide the dots and the last page if my current page is within 3 pages of the last one so that I don't get the last page number twice.?</p>

<p>Here's my current code</p>

<pre><code>{% paginate craft.entries.section('mySection').limit(10) as pageInfo, pageEntries %}

{% for page, url in pageInfo.getPrevUrls(3) %}
  &lt;a href="{{ url }}"&gt;{{ page }}&lt;/a&gt;
{% endfor %}

&lt;span class="current"&gt;{{ pageInfo.currentPage }}&lt;/span&gt;

{% for page, url in pageInfo.getNextUrls(3) %}
  &lt;a href="{{ url }}"&gt;{{ page }}&lt;/a&gt;
{% endfor %}

&lt;span&gt;...&lt;/span&gt;

&lt;a href="{{ pageInfo.lastUrl }}"&gt;{{pageInfo.totalPages}}&lt;/a&gt;
</code></pre>

<p>Thanks in advance. </p>

CC BY-SA 3.0
2017-11-24T17:44:43.690Z
Maybe I miss something but you have `entriesPage.totalPages` and you have `pageInfo.currentPage` why don't you just compare the numbers and if `totalPages - currentPage` is smaller than 3 you don't render the span and link
Robin Schambach
<templating><pagination><loop>
Getting the last X number of pages in pagination
# Getting the last X number of pages in pagination

(Asked by: username_0 on 2017-11-22)

I am working on a pagination for some articles. I am using a for loop to get the 3 pages after and before the current page .getPrevUrls(3) and .getNextUrls(3)
So far it looks like this, where 35 is the last page and the black one is the current page.

The problem I'm trying to fix is when the current page is within the 3 page range .getNextUrls(3) of the last page.

How can I create an if statement to hide the dots and the last page if my current page is within 3 pages of the last one so that I don't get the last page number twice.?
Here's my current code
{% paginate craft.entries.section('mySection').limit(10) as pageInfo, pageEntries %}

{% for page, url in pageInfo.getPrevUrls(3) %}
  {{ page }}
{% endfor %}

{{ pageInfo.currentPage }}

{% for page, url in pageInfo.getNextUrls(3) %}
  {{ page }}
{% endfor %}

...

{{pageInfo.totalPages}}

Thanks in advance.

**Tags:** templating, pagination, loop
> **Comments:**
> **username_1**: Maybe I miss something but you have `entriesPage.totalPages` and you have `pageInfo.currentPage` why don't you just compare the numbers and if `totalPages - currentPage` is smaller than 3 you don't render the span and link  

---

## 1 answers

### Answer by username_1 on 2017-11-27. Score: 6 | Accepted answer

As I already wrote in my comment 3 days ago something like this should work
{% paginate craft.entries.section('mySection').limit(10) as pageInfo, pageEntries %}

{% for page, url in pageInfo.getPrevUrls(3) %}
    {{ page }}
{% endfor %}

{{ pageInfo.currentPage }}

{% for page, url in pageInfo.getNextUrls(3) %}
    {{ page }}
{% endfor %}

{% if pageInfo.totalPages - pageInfo.currentPage > 3 %}
    ...

    {{pageInfo.totalPages}}
{% endif %}

[EDIT]: In the above script you will come across situations where the gap sometimes occurs between consecutive numbers, which can look silly.


To fix this you can add a little extra logic to it:
{% set amountVisible = amountVisible|default(2) %}
{% if pageInfo.currentPage > amountVisible + 1 %}
    1

    {% if pageInfo.currentPage == amountVisible + 3 %}
        2
    {% elseif pageInfo.currentPage > amountVisible + 3 %}
        ...
    {% endif %}
{% endif %}

{% for page, url in pageInfo.getPrevUrls(amountVisible) %}
    {{ page }}
{% endfor %}

{{ pageInfo.currentPage }}

{% for page, url in pageInfo.getNextUrls(amountVisible) %}
    {{ page }}
{% endfor %}

{% if pageInfo.totalPages - pageInfo.currentPage > amountVisible %}
    {% if pageInfo.totalPages - pageInfo.currentPage == amountVisible + 2 %}
        {{pageInfo.totalPages - 1}}
    {% elseif pageInfo.totalPages - pageInfo.currentPage > amountVisible + 1 %}
        ...
    {% endif %}

    {{pageInfo.totalPages}}
{% endif %}

Now this added logic will do two things:

If there is no gap, it won't render a thing.
If the gap only amounts to one page it will render a page number instead.

This results in the following:

As an added bonus: if you add the amountVisible variable on top with a default value you can put this in a twig partial which you can then easily implement on the paged view like so:
{% include 'partials/pager' with { pageInfo: pageInfo, amountVisible: 3 } %}
(Note that passing pageInfo explicitly is not required, but I do it for the sake of completeness and to prevent accidental cleanups of "unused" vars.)


---

