Download EE 2 Docs Download EE 1 Docs
ExpressionEngine

2.6.1 User Guide

Channel Entry and Comment Pagination

The pagination feature for both channel entries and comments works identically and allows you to display a limited number of entries and then automatically link to the next set. That way you can, for example, show comments 1-10 on the first page and automatically link to pages that display 11-20, 21-30, etc.

You have two choices as to the style of the navigation element. The first method would look something like this:

Page 27 of 344 pages  « First  <  11 12 13 14 15 >  Last »

The second method is a more traditional “next page” / “previous page” output:

Previous Page | Next Page

Pagination will also automatically restrict itself to any category you’re currently viewing. So if you have a category specified in your channel entries tag or you are viewing the entries of a category, then the pagination links will automatically restrict themselves to only entries in that category.

Example Code

Here are two basic code examples, one for each of the methods mentioned above. Information about the variables and parameters are covered later.

{exp:channel:entries channel="news" orderby="date" sort="desc" limit="1" paginate="bottom"}
    <h2>{title}</h2>
    {summary}
    {body}

    {paginate}
        <p>Page {current_page} of {total_pages} pages {pagination_links}</p>
    {/paginate}
{/exp:channel:entries}

It is important to note that it does not matter where you put your {paginate} code within the channel entries tag. The pagination code will be “stripped out” of the regular output and placed in the appropriate location according to what you specify with the paginate= parameter (detailed below).

And for the “next/previous” method

{exp:comment:entries channel="news" sort="desc" limit="1" paginate="bottom"}
    {comment}
    <p>By {name} on {comment_date format="%Y %m %d"}</p>
    {paginate}
        {if previous_page}
            <a href="{auto_path}">Previous Page</a> &nbsp;
        {/if}
        {if next_page}
            <a href="{auto_path}">Next Page</a>
        {/if}
    {/paginate}
{/exp:comment:entries}

Parameters

paginate=

paginate="top" paginate="bottom"  paginate="both"  paginate="inline"

This parameter determines where the pagination code will appear for your channel entries or comments:

  1. top: The navigation text and links will appear above your list of entries.
  2. bottom: The navigation text and links will appear below your list of entries.
  3. both: The navigation text and links will appear both above and below your list of entries.
  4. inline: The navigation text and links will appear within the list of entries for each entry.

If no parameter is specified, the navigation block will default to the “bottom” behavior.

Variable Pairs

paginate

{paginate}  {/paginate}

The opening and closing tags for pagination. This can to be used in conjunction with the paginate= parameter to determine where the contents of this tag will appear. See below for the available variables for use inside this tag. This tag is wrapped around either the single variables (see below) or the next/previous variable pairs.

if next_page

{if next_page}  {/if}

This tag will conditionally display the code inside the tag if there is a “next” page. If there is no next page then the content simply will not be displayed.

if previous_page

{if previous_page}  {/if}

This tag will conditionally display the code inside the tag if there is a “previous” page. If there is no previous page then the content simply will not be displayed.

Variables

These individual variables are for use inside the {paginate} tag pair.

auto_path

{auto_path}

The {auto_path} variable is used inside of the {if next_page} and {if previous_page} variable pairs. It is dynamically replaced with the correct path to the next/previous page. Unlike other “path” variables, this variable does not require the Template_Group/Template to be specified.

current_page

{current_page}

This variable is replaced by the page number of the current page you are viewing.

total_pages

{total_pages}

The total number of pages of channel entries or comments you have.

User Contributed Notes

Posted by: Forrest Anderson on 10 September 2012 12:47pm
Forrest Anderson's avatar

Important note on {pagination_links}. Whether you are using the single or the tag pair version, it must be in between the {paginate} tag pair or they won’t work.

{paginate}
  {pagination_links}
{
/paginate} 
{paginate}
  {pagination_links}
     Pagination details go here
...
  
{/pagination_links}
{
/paginate} 
Posted by: dehuszar on 29 June 2012 4:22pm
no avatar

In response to the piping conditional above, I think it’s more semantically clean to just add classes to your previous and next anchors (or containing list items) like so:

{paginate}

{if previous_page}
<class="prev" href="{auto_path}">Previous Page</a> &nbsp;
{/if}

{if {current_page} 
!= '1'   AND  "{current_page}" != "{total_pages}"  }&nbsp;|&nbsp;{/if}

{if next_page}
<class="next" href="{auto_path}">Next Page</a>
{/if}

{
/paginate} 

...and then use the following CSS

.prev + .next {
border
-left1em;
margin-left1em;
padding-left1em;

Not as much fun with EE tags I realize, but given that it’s a presentational detail and not content or structure, it’s cleaner markup, and might even shave a fraction of a second off your load times as it’s one less conditional to check.  I’ve used the above in basic next/prev pagination, and also ‘PREV | 1 2 3 4 5 | NEXT” style pagination as well.  Just wrap the numeric page links in an element with the class=“center” and use .prev + .center, .center + .next rules accordingly.

Posted by: Hop Studios on 8 June 2012 5:46pm
Hop Studios's avatar

When “pagination_links” is used as a single tag, here’s the simplest code:

{paginate}{pagination_links}{/paginate} 

Which outputs this:

<a href="URL">&lsaquoFirst</a>&nbsp;&nbsp;<a href="URL/P20"><</a>&nbsp;<a href="URL/P10">2</a>&nbsp;<a href="URL/P20">3</a>&nbsp;<strong>4</strong>&nbsp;<a href="URL/P40">5</a>&nbsp;<a href="URL/P50">6</a>&nbsp;<a href="URL/P40">></a>&nbsp;&nbsp;<a href="URL/P60">Last &rsaquo;</a

Which looks like this:
‹ First  < 2 3 4 5 6 >  Last ›

The “pagination_links” code pair can duplicate the output single tag, but it’s a lot more code.  Here’s the code for the pair that generates the same output (with a DIV wrapped around it for extra fun):

{paginate}
{if {total_pages} 
1}
<div class="pagination">
{pagination_links}
{first_page}
<a href="{pagination_url}">&lsaquoFirst</a>&nbsp;&nbsp;{/first_page}{previous_page}<a href="{pagination_url}"><</a>{/previous_page}{page}{if current_page}<strong>{pagination_page_number}</strong>{if:else}<a href="{pagination_url}">{pagination_page_number}</a>{/if}&nbsp;{/page}{next_page}<a href="{pagination_url}">></a>{/next_page}{last_page}<a href="{pagination_url}">Last &rsaquo;</a>{/last_page}
{
/pagination_links}
</div>
{/if}
{
/paginate} 

Please note the if… else for the current page—there’s no direct negation for current_page:

{if current_page}
  
<strong>{pagination_page_number}</strong>
{if:else}
  
<a href="{pagination_url}">{pagination_page_number}</a>
{/if} 
Posted by: vosSavant on 19 May 2012 8:26pm
vosSavant's avatar

To clarify my previous comment—this only seems to be an issue when working with 3rd party addons such as AB Entry IDs.

Posted by: vosSavant on 19 May 2012 7:30pm
vosSavant's avatar

If you find that pagination isn’t working, try adding the “paginate” parameter. The docs say it is optional and defaults to bottom, but I’ve found this is not necessarily the case.

Posted by: VIM Interactive on 8 November 2011 12:28pm
VIM Interactive's avatar

Please note: You must have at least 4 pages for the first_page and last_page variables to display.

For example:

1 2 3 Last 
  or
First 2 3 4 
Posted by: Florian Schroiff on 12 May 2011 12:16pm
Florian Schroiff's avatar

If you want to conditionally display a separator between previous and next page links you can use this:

{paginate}

{if previous_page}
<a href="{auto_path}">Previous Page</a> &nbsp;
{/if}

{if {current_page} 
!= '1'   AND  "{current_page}" != "{total_pages}"  }&nbsp;|&nbsp;{/if}

{if next_page}
<a href="{auto_path}">Next Page</a>
{/if}

{
/paginate} 

via: John Henry Donovan on the forums

Posted by: Brandon Kelly on 31 January 2010 6:23am
Brandon Kelly's avatar

If you only want to display the contents of your {paginate} tag pair when there’s more than one page, surround your tag contents with this condition:

{paginate}{if {total_pages} 1}
  
...
{/if}{/paginate} 

(Thanks to Pascal for the tip)

You must either have an EllisLab product license and have attained a forum rank of "Lab Assistant" (50 posts) to contribute notes to the User Guide