A quick pagination hack for Movable Type

Posted by Byrne, 27 May 2009

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:

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).'">&laquo; Older Posts</a>';
}
if ($page > 1) {
  echo '<a id="prev-page" href="?p='.($page - 1).'">Newer Posts &raquo;</a>';
}
?>
</div>

You see a live demo on Chicago Now...

Pros of this method

Cons

Anyways, I hope this hack helps you. If you have any questions, or need help, please let me know.

Filed in and tagged , , , , , , ,

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? ;-)

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 :-)

It's powerful! You can download modified templates from here (http://blog.godbus.com/2009/06/php-pagination-for-movable-type/).

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

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?

@Mike - try this to integrate pagination into Mid-Century: http://pastebin.com/f7cab622b

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?

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

It's always upsetting to hear about these kinds of things.

That's some pretty funny stuff ;)