Skip to content

Instantly share code, notes, and snippets.

@dewwwald
Last active August 29, 2015 14:01
Show Gist options
  • Save dewwwald/990e03302f66baaed876 to your computer and use it in GitHub Desktop.
Save dewwwald/990e03302f66baaed876 to your computer and use it in GitHub Desktop.
Pagination for Processwire

##Simple Pagination For Processwire

###ProcessWire Requirements

On the template you want pagination. Go to the url's tab where you will have to:

  • Activate Allow Page Numbers.
  • Activate Allow URL Segments.
<?php
//PAGINATION FOR PROCESSWIRE
//[pagename]-controller.php
/*************************************************************************************\
Remember to activate the pagination for the specific template.
\*************************************************************************************/
function simple_pagination() {
$limit = 4;
$page = wire('page');
$page_results = $page->children("limit={$limit}, sort=created");
$page_count = $page->children->count;
$total_pages = ceil($page_count / $limit);
if (has_pagination($total_pages)) {
$pagination = get_pagination($total_pages);
return $pagination;
}
return "false";
}
function has_pagination($total_pages) {
if ($total_pages <= 1) {
return false;
}
else {
return true;
}
}
function get_pagination($total_pages) {
$input = wire('input');
$page = wire('page');
$html = '';
$has_next = $input->pageNum < $total_pages;
$has_prev = $input->pageNum > 1;
if ($has_prev) {
$url = $page->url;
if ($input->pageNum > 2) {
$url .= "?page=".($input->pageNum - 1);
}
$html .= "<a href='{$url}'>Previous</a>";
}
if ($has_next) {
$html .= "<a href='{$page->url}?page=".($input->pageNum + 1)."'>Next</a>";
}
return $html;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment