Skip to content

Instantly share code, notes, and snippets.

@heesienooi
Created September 23, 2014 05:18
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heesienooi/de010131dce82685bb0f to your computer and use it in GitHub Desktop.
Save heesienooi/de010131dce82685bb0f to your computer and use it in GitHub Desktop.
Joomla pagination override - Reduce number of displayed pages
<?php
/**
* By default joomla display 10 pages in pagination. This file allows
* us to customize the number of displayed pages.
*
* Simply add this file to $template/html/pagination.php and
* change $displayedPages variable.
*/
defined('_JEXEC') or die('Restricted access');
/**
* Override joomla default pagination list render method
* @param array $list Pagination raw data
* @return string HTML string
*/
function pagination_list_render($list) {
$displayedPages = 6;
// Reduce number of displayed pages to 6 instead of 10
$list['pages'] = _reduce_displayed_pages($list['pages'], $displayedPages);
return _list_render($list);
}
/**
* Reduce number of displayed pages in pagination
* @param array $pages Pagination pages raw data
* @param integer $displayedPages Number of displayed pages
* @return string HTML string
*/
function _reduce_displayed_pages($pages, $displayedPages) {
$currentPageIndex = _get_current_page_index($pages);
$midPoint = ceil($displayedPages / 2);
if ($currentPageIndex >= 6) {
$pages = array_slice($pages, -$displayedPages);
} else {
$startIndex = max($currentPageIndex - $midPoint, 0);
$pages = array_slice($pages, $startIndex, $displayedPages);
}
return $pages;
}
/**
* Get current page index
* @param array $pages Pagination pages raw data
* @return integer Current page index
*/
function _get_current_page_index($pages) {
$counter = 0;
foreach ($pages as $page) {
if (!$page['active']) return $counter;
$counter++;
}
}
/**
* Function copied from joomla html pagination to render pagination data into html string
* @param array $list Pagination raw data
* @return string HTML string
*/
function _list_render($list) {
// Reverse output rendering for right-to-left display.
$html = '<ul>';
$html .= '<li class="pagination-start">' . $list['start']['data'] . '</li>';
$html .= '<li class="pagination-prev">' . $list['previous']['data'] . '</li>';
foreach ($list['pages'] as $page)
{
$html .= '<li>' . $page['data'] . '</li>';
}
$html .= '<li class="pagination-next">' . $list['next']['data'] . '</li>';
$html .= '<li class="pagination-end">' . $list['end']['data'] . '</li>';
$html .= '</ul>';
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment