Pixel & Tonic

Matrix

version works on just rated
2.0.9 EE1 & EE2 $55
Buy Now Get Help

Pagination

Although Matrix does not support any sort of {paginate} tag pair, you can still achieve pagination with a little effort. This guide will walk you through the basics.

Prerequisites

Before we begin, make sure you have the following installed:

The Template

First, let’s define a variable that defines how many Matrix rows we wish to display per-page:

{assign_variable:rows_per_page="10"} /* EE1 */
{preload_replace:rows_per_page="10"} /* EE2 */

Then, add two parameters to your Matrix opening tag, offset and limit:

{gallery offset="{segment_4}" limit="{rows_per_page}"}
  <img src="{img}" width="{width}" height="{height}" alt="{alt}" />
{/gallery}

offset tells Matrix which row it should begin with. We’re setting it to the first undefined URL segment, so that on subsequent pages, defining this starting point is as simple as adding a number to the end of the URL.

limit tells Matrix how many rows to display, after the predefined offset.

Now that the Matrix field is ready for pagination, it’s time to set up the pagination links. This is where the Simple Math/MX Calculator plugins come in handy:

{if segment_4 > rows_per_page}
  <a href="/path/to/gallery/{exp:mx_calc expression='{segment_4}-{rows_per_page}'}">
    Prev
  </a>
{/if}

{if "{gallery:total_rows}" > "{exp:mx_calc expression='{segment_4}+{rows_per_page}'}"}
  <a href="/path/to/gallery/{if segment_4}{exp:mx_calc expression='{segment_4}+{rows_per_page}'}{if:else}{rows_per_page}{/if}">
    Next
  </a>
{/if}

Note: If you’re using EE1, replace all instances of “{exp:mx_calc expression=” with “{exp:simple_math calculate=”.

As you can see, we are only showing the Previous and Next links if they should be displayed, and their link URLs simply subtract or add the predefined {rows_per_page} to {segment_4}.

There’s plenty of room for improvement, such as using {if:else} conditionals to display pagination link fallbacks, etc., but this alone is enough to get you started.