I looked around the forums and couldn’t find a solution to my problem, so I decided the roll up my sleeves and make my own solution.
Problem: using the pages tag displays all pages when using pagination, and I only want to show a maximum of 7 (the current page, 3 below and 3 above).
The solution (written in 0.9.6.3 with ditto 2.1):
Step 1. Add the following parameter definition to the snippet code in the manager:
$pagesPerPage = isset($pagesPerPage) ? $pagesPerPage : 0;
/*
Param: pagesPerPage
Purpose:
Number of pages to display in pagination
Options:
Any number
Default:
0
*/
Step 2: Also in the snippet code, add this parameter to $ditto->paginate call:
$ditto->paginate($start, $stop, $total, $display, $tplPaginateNext, $tplPaginatePrevious, $tplPaginateNextOff, $tplPaginatePreviousOff, $tplPaginatePage, $tplPaginateCurrentPage, $paginateAlwaysShowLinks, $paginateSplitterCharacter, $pagesPerPage);
Step 3: Change the parameter list in the paginate function definition in assets/snippets/ditto/classes/ditto.class.inc.php:
function paginate($start, $stop, $total, $summarize, $tplPaginateNext, $tplPaginatePrevious, $tplPaginateNextOff, $tplPaginatePreviousOff, $tplPaginatePage, $tplPaginateCurrentPage, $paginateAlwaysShowLinks, $paginateSplitterCharacter, $pagesPerPage) {
Step 4: In the same file, replace the code:
for ($x = 0; $x <= $totalpages -1; $x++) {
$inc = $x * $summarize;
$display = $x +1;
if ($inc != $start) {
$pages .= $this->template->replace(array('url'=>$this->buildURL("start=$inc"),'page'=>$display),$tplPaginatePage);
} else {
$modx->setPlaceholder($dittoID."currentPage", $display);
$pages .= $this->template->replace(array('page'=>$display),$tplPaginateCurrentPage);
}
}
with:
$begin = 0;
$end = $totalpages - 1;
$tmpCurrentPage = $start / $summarize;
$pagesPrefix = '...';
$pagesPostfix = '...';
if ($pagesPerPage && $pagesPerPage < $totalpages)
{
if ($tmpCurrentPage < $pagesPerPage / 2)
{
$end = $pagesPerPage - 1;
}
elseif ($tmpCurrentPage > $totalpages - ($pagesPerPage / 2))
{
$begin = $totalpages - $pagesPerPage;
}
else
{
$begin = ceil($tmpCurrentPage - ($pagesPerPage / 2));
$end = floor($tmpCurrentPage + ($pagesPerPage / 2));
}
}
if ($begin <= 0)
{
$pagesPrefix = '';
}
if ($end >= $totalpages-1)
{
$pagesPostfix = '';
}
for ($x = $begin; $x <= $end; $x++) {
$inc = $x * $summarize;
$display = $x +1;
if ($inc != $start) {
$pages .= $this->template->replace(array('url'=>$this->buildURL("start=$inc"),'page'=>$display),$tplPaginatePage);
} else {
$modx->setPlaceholder($dittoID."currentPage", $display);
$pages .= $this->template->replace(array('page'=>$display),$tplPaginateCurrentPage);
}
}
$pages = $pagesPrefix . $pages . $pagesPostfix;
Step 5: Use it. You can now use the parameter pagesPerPage as in the following snippet call.
[!Ditto? &sortBy=`pub_date` &sortDir=`DESC` &summarize=`5` &paginate=`1` &paginateAlwaysShowLinks=`1` &pagesPerPage=`7` !]