A quick pagination hack for Movable Type
One of the most common feature requests for Movable Type is entry pagination. It is actually a sticky problem for a system that utilizes static publishing like Movable Type though. As an aside, I am become very unfond of the term "static publishing" and am coming around to what I think is a more apt term: "pre-emptive caching." Anyways, there are a number of well supported techniques for pagination, and the one I send most people to is Mark Carey's Pagination plugin. But when people feel that is overkill, I often help them implement the following hack.
The hack is simple when you think about it:
- Create a template that outputs a relatively large number of entries.
- Segment the entries into several groups.
- Using some simple template code, wrap each group of entries with a simple PHP statement that will only be evaluated according to the current page being viewed.
It is simple and quick to implement. For example, here is the code I use on Chicago Now to achieve some simple pagination:
<mt:var name="currentp" value="0">
<mt:var name="limit" value="10">
<mt:var name="lastn" value="30"><?php
$page = $_REQUEST['p'];
if (!isset($page)) { $page = 1; }
?>
<mt:Entries lastn="$lastn">
<mt:setvarblock name="page"><mt:getvar name="__counter__" op="mod" value="$limit"></mt:setvarblock>
<mt:if name="page" eq="1">
<mt:setvar name="currentp" op="add" value="1">
<mt:if name="currentp" gt="1"><?php } ?></mt:if>
<?php if ($page == <$mt:var name="currentp"$>) { ?>
</mt:if>
<$mt:include module="Entry Summary"$>
<mt:if name="__last__"><?php } ?></mt:if>
</mt:Entries>
<div id="pagination">
<?php
$limit = <mt:var name="limit">;
$page_count = <mt:var name="currentp">;
if ($page < $page_count) {
echo '<a id="next-page" href="?p='.($page + 1).'">« Older Posts</a>';
}
if ($page > 1) {
echo '<a id="prev-page" href="?p='.($page - 1).'">Newer Posts »</a>';
}
?>
</div>
You see a live demo on Chicago Now...
Pros of this method
- Its free.
- Fast to implement
- Fast page load times
- More page views for your site and more ad impressions
- Achieves desired objective
Cons
- The more pages you want your users to be able to navigate back in time, the longer it will take to publish, cough I mean cache the page.
- You can't really go back in time infinitely. You have to choose a reasonable number of pages to allow your users to paginate through. On the plus side, most users most navigate that far back - rarely more than 10 pages in fact.
Anyways, I hope this hack helps you. If you have any questions, or need help, please let me know.
Filed in Movable Type and tagged chicagonow, demo, fast, free, hacks, movable type, pagination, php
10 Comments
I used Alden Bate's Paged Archives Plugin recently. Free, easy to implement and well thought.
(Very nice live comment preview BTW, where's the tutorial? ;-)
François Nonnenmacher at 2009-05-28T01:09:22-08:00
Thanks for the tutorial on doing this. I've seen this mentioned by a few MT devs but since i'm such a hack at php I could never figure it out. This will be very helpful on a few projects!
I'd also like to know how you're doing the comment preview :-)
kimonostereo at 2009-05-30T15:00:35-08:00
It's powerful! You can download modified templates from here (http://blog.godbus.com/2009/06/php-pagination-for-movable-type/).
Jackie at 2009-06-03T08:23:45-08:00
I have a quick question Byrne. I've attempted to use this code in the mid-century theme but it seems to try to render the php as text. Here's what I've got so far in my main index template.. how do I use your code / where do I put it?
http://pastebin.com/m6d2244d3
Mike Johnston at 2009-07-07T20:35:17-08:00
Thanks for the pastebin link, but I do not see any PHP code in your templates. If what you describe is correct though, then your web server must not be processing your files as PHP. Are they being published with a .php file extension?
Byrne at 2009-07-08T00:34:04-08:00
@Mike - try this to integrate pagination into Mid-Century: http://pastebin.com/f7cab622b
Byrne at 2009-07-15T10:20:53-08:00
Byrne, thanks for this hack. I'm having trouble getting it to render the pagination links in an index template when I add any any category info (single or multiple) to the tag.
Any advice?
Chad at 2009-08-04T17:43:26-08:00
Apologies. Client unpublished many entries leaving me to think pagination wasn't working. All's well and this hack does great for me in MT 4.3, FYI
Chad
chad at 2009-08-04T21:51:57-08:00
It's always upsetting to hear about these kinds of things.
Greeson at 2009-08-22T03:05:19-08:00
That's some pretty funny stuff ;)
Tremblay at 2009-08-22T03:31:21-08:00