Movable Type 4.3 offers pagination for various archive pages, however one feature which has not been added is pagination for single entries.
Lets say you have a very long entry that you want broken up into 2, 3 or more pages, there's currently no support for that other than a plugin, such as Pagination from MT Hacks. While that plugin has support for multi-page entries, it also includes a lot of 'extra stuff' and displays the additional pages via dynamic page loads.
If you are looking for something small and easy to create multi-page entries, it can be done with just a little PHP in your Entry archive template.
All you need for this to work is,
- Entry pages with PHP extension, or other extension that's parsed by PHP.
- Static publishing – will not work with dynamic publishing of entries.
If you already have a blog using .html extensions and don't want to change your links, here's a quick tutorial on how to parse .html with PHP.
Benefits of this method:
- Allows for an unlimited amount of pages per entry.
- Does not increase the publishing duration of your posts.
- Works with all versions of Movable Type, and upgrading will not affect it.
- No hassles with plugins, upgrades or MT modifications.
- Uses virtually no extra server resources and does not slow page loads.
Open the Entry Archive template, and find the MT tag <$mt:EntryBody$>. If you can't find it, it may be in an included template module.
Here's the PHP code to replace that tag:
<?php
$entrytext = <<<EOF
<$mt:EntryBody$>
EOF;
Basically what this does is put your entry text into a PHP variable which we can manipulate and display in parts.
Now we can split the entry into parts with:
$parts = explode("<!--more-->", $entrytext);
In this case we're using <!--more--> to split the text. I'm using that as the example because it's the same divider used by WordPress, so if you happen to be importing content from WordPress, you would still be able to keep your pagination breaks.
So, for each page you want in your entry, you can just add <!--more--> tag into your entry.
Now we get the current page, and determine which part of the text to display:
$currentPage = (isset($_GET['p'])) ? $_GET['p'] : 1;
$currentPart = $currentPage - 1;
Then we can display the content part based on the page:
echo $parts[$currentPart];
The last part is adding the pagination links to next and previous pages:
if ( $currentPage > 1 ) {
$prev = $currentPage - 1;
echo '<a href="<$mt:EntryPermalink$>?p=' . $prev . '">< Previous Page</a>';
}
if ( count( $parts ) > 1 ) {
echo ' Page ' . $currentPage . ' of ' . count( $parts ) . ' ';
}
if ( $currentPage < count( $parts ) && count( $parts ) > 1 ) {
$next = $currentPage + 1;
echo '<a href="<$mt:EntryPermalink$>?p=' . $next . '">Next Page ></a>';
}
?>
If you don't understand whats going on above, that's ok, just copy and paste that into your template.
That's it, you now have pagination for your long entries. You can see a working example here.
If you're familiar with PHP, you can modify the next/previous links any way you want. Lets say you want to show each page number as a link, here's an example code for that:
echo 'Page: ';
for ( $i=1;$i<=count($parts);$i++ ) {
if ( $currentPage == $i ) echo $i . ' ';
else echo '<a href="<$mt:EntryPermalink$>?p=' . $i . '">' . $i . '</a> ';
}
The above code will list each page like:
Page: 1 2 3 4 5
Here's the full code, this can just replace the <$mt:EntryBody$> tag in your template:
<?php
$entrytext = <<<EOF
<$mt:EntryBody$>
EOF;
$parts = explode("<!--more-->", $entrytext);
$currentPage = (isset($_GET['p'])) ? $_GET['p'] : 1;
$currentPart = $currentPage - 1;
echo $parts[$currentPart];
if ( $currentPage > 1 ) {
$prev = $currentPage - 1;
echo '<a href="<$mt:EntryPermalink$>?p=' . $prev . '">< Previous Page</a>';
}
if ( count( $parts ) > 1 ) {
echo ' Page ' . $currentPage . ' of ' . count( $parts ) . ' ';
}
if ( $currentPage < count( $parts ) && count( $parts ) > 1) {
$next = $currentPage + 1;
echo '<a href="<$mt:EntryPermalink$>?p=' . $next . '">Next Page ></a>';
}
?>