ExpressionEngine

2.5.0 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"

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.

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: vosSavant on 19 May 2012 7: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 6: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 11:28am
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 11:16am
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 5: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 have an ExpressionEngine license and have attained a forum rank of "Lab Assistant" (100 posts) to contribute notes to the User Guide