1

I understand I can use a conditional to output a 'next page' link only when next pages exist, but I am finding a problem with direct URLs.

Let's say you have 20 entries and 10 per page.

The URL /news (or /news/p1) and /news/p2 should work, but I would expect /news/p3 to show no results or a message.

What I'm finding however is that /news/p3 and beyond returns the last entry in the collection.

Here's the template code:

{% paginate craft.entries.section(listingSection).order('postDate desc').limit(2) as entriesOnPage %}

    <div class="ral-loading ral-hero-block ral-headline">Loading the latest content...</div>

    <div class="isotope js-isotope" >

        {% for entry in entriesOnPage %}

            {% if entry.section.handle != 'tweet' %}

                {% switch entry.tileSize %}

                    {% case "Single" %}

                        {% include '_partials/_isotope_block_single.html' %}

                    {% case "Double Horizontal" %}

                        {% include '_partials/_isotope_block_horizontal.html' %}

                    {% case "Double Vertical" %}

                        {% include '_partials/_isotope_block_vertical.html' %}

                    {% case "Quadruple" %}

                        {% include '_partials/_isotope_block_big.html' %}

                    {% case "Quote" %}

                        {% include '_partials/_isotope_block_quote.html' %}

                {% endswitch %}

            {% else %}

                {% include '_partials/_isotope_block_tweet.html' %}

            {% endif %}

        {% endfor %}

    </div>

    {% if paginate.nextUrl %}

        <div class="ral-load-more-posts nav-previous">

            <div class="container">

                <a class="ral-load-more-posts-text" href="{{ paginate.nextUrl }}">Load more posts</a>

            </div>

        </div>

    {% endif %}

{% endpaginate %}
4
  • Can you share your pagination template code?
    – Brad Bell
    Commented Jan 5, 2015 at 16:29
  • I've edited the question to include the template code.
    – Russ Back
    Commented Jan 5, 2015 at 16:31
  • Curious... what's the purpose of the limit(2) in the opening paginate block? It might not be doing what you think it's doing.
    – Brad Bell
    Commented Jan 5, 2015 at 18:12
  • I want to set a number of entries per page, in this case 2.
    – Russ Back
    Commented Jan 5, 2015 at 19:57

1 Answer 1

1

You can detect if the requested page is greater than the total available pages for the current list of entries like so:

{% paginate entries as entriesOnPage %}

    {% if craft.request.getPageNum() > paginate.totalPages %}
        <p>No results</p>
    {% else %}
        {% for entry in entriesOnPage %}
            ...
        {% endfor %}
    {% endif %}

{% endpaginate %}

Rather than displaying a message, you could also choose to put an {% exit 404 %} tag in there.

0

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.