Next and Previous

February 14th, 2011 | Filed in: Custom MySQL • First party add-ons | Comment on This Post

Expression Engine has robust support built in for pagination, including "next" and "previous" links for individual entries. Designed to work on "single entry" pages, it's an effective way to allow browsing through entries without needing to go back to a list of entries. In some cases however, for example smaller sites that don't have the content to back up the development time dedicated to a more complex template group/template scenario, we typically code a single template using a simple conditional:

{if segment_1 == "" || segment_1 == "home"}
{exp:channel:entries channel="channel_name"}
<h1>{title}</h1>
<p><a href="{title_permalink}"> title="{title}">{title}</a></p>
{/exp:channel:entries}
{if:else}
{exp:channel:entries url_title="{segment_1}"}
<h1>{title}</h1>
<p>{body}</p>
{/exp:channel:entries}
{/if}

The result is a clean URL structure (www.site.com/entry-title/) and perfect for those smaller sites where complex URIs like /news/entry/entry-title feel a bit over the top. Unfortunately, using this set up doesn't allow for the native EE "next" and "previous" links. The easy solution of course is to dedicate a template to entries- but consider the following URL choices:
www.author.com/poem-name
www.author.com/poetry/poem/poem-name
Is the development time and complex URI structure worth it?

It is possible to achieve both a simple URI and "next" "previous" links with a little help from Expression Engine's query tag.

In our template, within the second part of the "if" statement we put the following:

{embed="embed/.page" e_id="{entry_id}"}

Using an embedded template allows us to work outside of the current channel entry tag but work with the information it has provided. We create a variable "e_id" for the current entry's entry_id and send it to our custom queries to create "next" and "previous" links:

{exp:query sql="SELECT title,entry_id,url_title FROM exp_channel_titles WHERE channel_id = '2' AND entry_id > '{embed:e_id}' ORDER BY entry_id ASC"}
{if count == "1"}<a href="{site_url}{url_title}" title="{title}" id="next_entry" class="next">Next</a>{/if}
{/exp:query}
{exp:query sql="SELECT title,entry_id,url_title FROM exp_channel_titles WHERE channel_id = '2' AND entry_id < '{embed:e_id}' ORDER BY entry_id DESC"}
{if count == "1"}<a href="{site_url}{url_title}" title="{title}" id="prev_entry" class="prev">Previous</a>{/if}
{/exp:query}

In this case we're limiting the channel so that the query doesn't pick up entries from other channels- an unnecessary step if your site is only using one channel. The query for "next" is ordered ascending, looking for any entry IDs greater than the current entry. The Query for "previous" is ordered descending, looking for any entry IDs less than the current entry. Within each query we use a simple conditional to identify and limit the results the first entry that fits the bill.

An added advantage to this set up is the lack of PHP in the templates- it's all native EE!

Browse Categories

Share This Article

Comment On This Article

Name:
Email:
Website:
Enter the text from the image above:

Comments:

Contact Us

Would you like to discuss a project, or are you interested in a quote? Contact us and let us know how we can help you with your next print or web design project. We are fast, cheap and easy.

Adam Lurie
Project Developer
Email Adam

Eric Nardo
Creative Director
Email Eric

Send Us A Message

Web Development Practices

We use the ExpressionEngine® content management system. ExpressionEngine® is an extensible software, easily deployed and highly customizable. We trust EE to run all of our websites.

Web Design Practices

We do our best to hand-craft websites that function properly and adhere to web standards. Practicing sensible mark-up and design promotes website usability & accessibility.

Chicago Illinois | © 2010 - 2011 Peoples Elbow. All Rights Reserved.