<p>First, I don’t think your pagination navigation is limiting to just 6 pages like you think it is. The number you pass into <code>paginate.getPrevUrls()</code> and <code>paginate.getNextUrls()</code> will determine how many URLs the functions should return. So if you’re on the 3rd page, calling <code>paginate.getNextUrls(5)</code> will give you the URLs for pages 4-8.</p>

<p>Navigation-wise, what you’re probably better off using is <code>paginate.getRangeUrls()</code>, which accepts explicit first/last page numbers:</p>



<pre class="lang-twig prettyprint-override"><code>{% for page, url in paginate.getRangeUrls(1, 6) %}
    {% if page == paginate.currentPage %}
        &lt;strong&gt;{{ page }}&lt;/strong&gt;
    {% else %}
        &lt;a href="{{ url }}" class="button"&gt;{{ page }}&lt;/a&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p><em>(You can get a full breakdown of the properties/functions within that <code>paginate</code> variable here: <a href="http://buildwithcraft.com/docs/templating/paginate#the-paginate-variable)_" rel="nofollow">http://buildwithcraft.com/docs/templating/paginate#the-paginate-variable)</em></a></p>

<p>Even with that change, you’re not actually <em>enforcing</em> that only 6 pages are available; there’s still nothing stopping a user from manually going to <a href="http://example.com/news/p7" rel="nofollow">http://example.com/news/p7</a>. The would still get the 7th page of news entries, with an awkward pagination navigation, because it would just show links to pages 1-6 with no indication that you’re on the 7th page.</p>

<p>So back to your original question: how do we enforce a 6-page maximum?</p>

<p>Your idea of setting a route in craft/config/routes.php was a good one, but unfortunately Craft actually removes the “page” part of the URL from its internal path before it starts checking the path for matching routes.</p>

<p>Because of that, your best option here is to enforce it from the actual News template.</p>

<p>You can access the current page using <a href="http://buildwithcraft.com/docs/templating/craft.request#getPageNum" rel="nofollow"><code>craft.request.getPageNum()</code></a>, so let’s just check for that at the very top of your template, and if it’s greater than 6, return a 404 using the <a href="http://buildwithcraft.com/docs/templating/exit" rel="nofollow"><code>{% exit %}</code></a> tag:</p>

<pre class="lang-twig prettyprint-override"><code>{% if craft.request.getPageNum() &gt; 6 %}
    {% exit 404 %}
{% endif %}
</code></pre>

<p>To clean things up a bit, you might want to store that maximum number of News entries as a variable within your template, or maybe even as a <code>'maxNewsPages'</code> custom config variable in craft/config/general.php.</p>

<pre class="lang-twig prettyprint-override"><code>{# Pull the custom 'maxNewsPages' config setting from craft/config/general.php #}
{% set maxPages = craft.config.maxNewsPages %}

{# Enforce it #}
{% if craft.request.getPageNum() &gt; maxPages %}
    {% exit 404 %}
{% endif %}

...

{% for page, url in paginate.getRangeUrls(1, maxPages) %}
    {% if page == paginate.currentPage %}
        &lt;strong&gt;{{ page }}&lt;/strong&gt;
    {% else %}
        &lt;a href="{{ url }}" class="button"&gt;{{ page }}&lt;/a&gt;
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-10-23T10:03:00.053Z
Wow, a perfect answer - thank you hugely Brandon!
jezmck
CC BY-SA 3.0
2014-10-23T10:04:51.947Z
For the record, none of the existing code was mine in the first place, it's just been given to me.
jezmck
CC BY-SA 3.0
2014-10-23T10:06:31.457Z
@jezmck Glad that helps! The old code probably worked fine for the previous requirements :)
Brandon Kelly
CC BY-SA 3.0
2014-10-23T09:49:02.010Z
2014-10-23T09:49:02.010Z
Brandon Kelly
<p>I've been given a site and asked to prevent visitors from seeing the news archive beyond page 6.</p>

<p>The navigation has already been done, but if the user manually enters <code>example.com/news/p7</code> they'll see it.</p>

<p>Here’s my template code:</p>

<pre><code>{% set entries = craft.entries.section('news').limit(3) %}
{% paginate entries as entries %}
    {% for entry in entries %}
        {% include '_elements/_news-entry' %}
    {% endfor %}
    {% include '_elements/_pagination' %}
{% endpaginate %}
</code></pre>

<p>The _elements/_pagination include:</p>

<pre><code>&lt;nav class="pagination"&gt;
{% for page, url in paginate.getPrevUrls(5) %}
    &lt;a href="{{ url  }}" class="button"&gt;{{ page }}&lt;/a&gt;
{% endfor %}

&lt;strong&gt;{{ paginate.currentPage }}&lt;/strong&gt;

{% for page, url in paginate.getNextUrls(5) %}
    &lt;a href="{{ url  }}" class="button"&gt;{{ page }}&lt;/a&gt;
{% endfor %}
&lt;/nav&gt;
</code></pre>

<p>I tried adding the following to <code>/craft/config/routes.php</code>, but it doesn't appear to have any effect.</p>

<pre><code>'news/p[^1-6]' =&gt; '404',
</code></pre>

<entries><routes><pagination><404-page>
Limit entry pagination to 6 pages?
# Limit entry pagination to 6 pages?

(Asked by: username_0 on 2014-10-23)

I've been given a site and asked to prevent visitors from seeing the news archive beyond page 6.
The navigation has already been done, but if the user manually enters example.com/news/p7 they'll see it.
Here’s my template code:
{% set entries = craft.entries.section('news').limit(3) %}
{% paginate entries as entries %}
    {% for entry in entries %}
        {% include '_elements/_news-entry' %}
    {% endfor %}
    {% include '_elements/_pagination' %}
{% endpaginate %}

The _elements/_pagination include:

{% for page, url in paginate.getPrevUrls(5) %}
    {{ page }}
{% endfor %}

{{ paginate.currentPage }}

{% for page, url in paginate.getNextUrls(5) %}
    {{ page }}
{% endfor %}


I tried adding the following to /craft/config/routes.php, but it doesn't appear to have any effect.
'news/p[^1-6]' => '404',

**Tags:** entries, routes, pagination, 404-page

---

## 1 answers

### Answer by username_1 on 2014-10-23. Score: 4 | Accepted answer

First, I don’t think your pagination navigation is limiting to just 6 pages like you think it is. The number you pass into paginate.getPrevUrls() and paginate.getNextUrls() will determine how many URLs the functions should return. So if you’re on the 3rd page, calling paginate.getNextUrls(5) will give you the URLs for pages 4-8.
Navigation-wise, what you’re probably better off using is paginate.getRangeUrls(), which accepts explicit first/last page numbers:
{% for page, url in paginate.getRangeUrls(1, 6) %}
    {% if page == paginate.currentPage %}
        {{ page }}
    {% else %}
        {{ page }}
    {% endif %}
{% endfor %}

(You can get a full breakdown of the properties/functions within that paginate variable here: http://buildwithcraft.com/docs/templating/paginate#the-paginate-variable)
Even with that change, you’re not actually enforcing that only 6 pages are available; there’s still nothing stopping a user from manually going to http://example.com/news/p7. The would still get the 7th page of news entries, with an awkward pagination navigation, because it would just show links to pages 1-6 with no indication that you’re on the 7th page.
So back to your original question: how do we enforce a 6-page maximum?
Your idea of setting a route in craft/config/routes.php was a good one, but unfortunately Craft actually removes the “page” part of the URL from its internal path before it starts checking the path for matching routes.
Because of that, your best option here is to enforce it from the actual News template.
You can access the current page using craft.request.getPageNum(), so let’s just check for that at the very top of your template, and if it’s greater than 6, return a 404 using the {% exit %} tag:
{% if craft.request.getPageNum() > 6 %}
    {% exit 404 %}
{% endif %}

To clean things up a bit, you might want to store that maximum number of News entries as a variable within your template, or maybe even as a 'maxNewsPages' custom config variable in craft/config/general.php.
{# Pull the custom 'maxNewsPages' config setting from craft/config/general.php #}
{% set maxPages = craft.config.maxNewsPages %}

{# Enforce it #}
{% if craft.request.getPageNum() > maxPages %}
    {% exit 404 %}
{% endif %}

...

{% for page, url in paginate.getRangeUrls(1, maxPages) %}
    {% if page == paginate.currentPage %}
        {{ page }}
    {% else %}
        {{ page }}
    {% endif %}
{% endfor %}

> **Comments:**
> **username_0**: Wow, a perfect answer - thank you hugely Brandon!  
> **username_0**: For the record, none of the existing code was mine in the first place, it's just been given to me.  
> **username_1**: @jezmck Glad that helps! The old code probably worked fine for the previous requirements :)  

---

