Lisa Wess
VP of Operations

The Importance of Semantics: Pagination

One thing that I did not cover when going through The Importance of Semantics was how Pagination works.  The various types of pagination all rely on the URL to know which entries are displayed.  We’ve had a bit of a break from the Semantics series, but why don’t we dig into how the Pagination works now?

Remember, with our Semantics series, we always start with the same code:

{exp:channel:entries channel="default_site" limit="10" disable="pagination"}
<h2>{title}</h2>
{body}
{
/exp:channel:entries} 

Add this pagination code immediately before {/exp:channel:entries}:

{paginate}
<p>Page {current_page} of {total_pages} pages {pagination_links}</p>
{/paginate} 

So your template should now look like this:

{exp:channel:entries channgel="default_site" limit="10" disable="pagination"}
<h2>{title}</h2>
{body}

{paginate}
<p>Page {current_page} of {total_pages} pages {pagination_links}</p>
{/paginate}

{
/exp:channel:entries} 

Now, visit that template.  For example, if that template is site/index, visit it here:

http://www.example.com/site/index/

You will see your Pagination links.  Click on any of the pagination links, for instance the number 2. You’ll note that with a limit=“10”, when you click the pagination link, you now get P10 appended to the URL.  This tells ExpressionEngine to show the second page of 10 entries. If you click on 1 again, you get P0 - the first page of 10 entries, with 3 for P20 - the third page of 10 entries.  Now, if you change your limit to say limit=“7” then you’ll note that the pagination segment is in multiples of 7 instead of 10. 

So here is what that tells us: P tells ExpressionEngine that it is paginating a multi-entry template. The number is a multiple of the limit= parameter, telling ExpressionEngine directly what entries to show. 

It’s important to mention here that if you are using dynamic=“off” you will be turning off pagination.  The links will still be generated, but since dynamic=“off” tells ExpressionEngine to ignore the URL, the list of entries will not actually paginate.  So do keep that in mind!

Pagination for single-entry pages is a little different.  Instead of your above code, let’s give this a shot, from the Next/Prev Entry Linking documentation

{exp:channel:entries channel="default_site" limit="10" disable="pagination"}
<h2>{title}</h2>
{body}
{
/exp:channel:entries}

{exp
:channel:next_entry}
<p>Next entry: <a href="{path="site/comments"}">{title}</a></p>
{/exp:channgel:next_entry}

{exp
:channel:prev_entry}
<p>Previous entry: <a href="{path="site/comments"}">{title}</a></p>
{/exp:channel:prev_entry} 

These tags should not be nested, they get their context from being on a single-entry page.  In this case, ExpressionEngine just looks at the article you’re on, and the articles before and after, and creates the links automatically, using your standard permanent link template for the display.  This is still reliant on the URL to know that you’re on a single-entry page, of course.

It feels good to come back to this series occasionally and fill it in!  I hope everyone is enjoying 2008!