6

I am trying to paginate grouped entries as follows...

{% set searchParam = craft.request.getParam('q') %}

{% set groups = craft.entries({
    search: searchParam
})|group('section') %}

{% for entries in groups %}
    {% paginate entries.limit(10) as pageEntries %}

    {% endpaginate %}
{% endfor %}

However it is throwing this error on the paginate line: Array to string conversion

How else can I achieve this functionality?

  • Where do you want to break pages? After x entries or after x groups? If entries, a group from page 1 most likely gets continued on page 2 and you need additional group headings etc.? – carlcs Apr 17 '15 at 11:53
2

That error is occurring at entries.limit(10), because at that point entries is already an array of fetched entries, rather than an ElementCriteriaModel. (The entries will get fetched as soon as the ElementCriteriaModel is passed to your |group filter.)

The only way to grab just 10 entries per section (per page) will be to loop through each of your sections, and create a new ElementCriteriaModel for each one.

{% set sections = {
    'news': 'News',
    'events': 'Events',
    // ...
} %}

{% for section, sectionName in sections %}

    {% set entries = craft.entries.section(section).limit(10) %}
    {% paginate entries as pageInfo, pageEntries %}

    {% if pageEntries|length %}
        <h3>{{ sectionName }}</h3>

        {% for entry in pageEntries %}
            <a href="{{ entry.url }}">{{ entry.title }}</a>
        {% endfor %}
    {% endif %}

{% endfor %}

Note that Craft does not support multiple independently paginated lists of entries on a single webpage. So if you use the built-in {% paginate %} tag like this, when it takes you to page 2, all of the sections’ {% paginate %} tags will start showing page 2 content. And you’ll need to keep track of which section actually has the most pages, before building out your pagination nav.

// ...

{% set primaryPageInfo = null %}

{% for section, sectionName in sections %}

    {% set entries = craft.entries.section(section).limit(10) %}
    {% paginate entries as pageInfo, pageEntries %}

    // ...

    {# Does this section have the most entries (so far)? #}
    {% if not primaryPageInfo or pageInfo.totalPages > primaryPageInfo.totalPages %}
        {% set primaryPageInfo = pageInfo %}
    {% endif %}

{% endfor %}

{# Build out the pagination nav using the biggest section's pageInfo #}
<a href="{{ primaryPageInfo.firstUrl }}">First Page</a>
{% if primaryPageInfo.prevUrl %}<a href="{{ primaryPageInfo.prevUrl }}">Previous Page</a>{% endif %}
{% if primaryPageInfo.nextUrl %}<a href="{{ primaryPageInfo.nextUrl }}">Next Page</a>{% endif %}
<a href="{{ primaryPageInfo.lastUrl }}">Last Page</a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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