dewwwald
5/28/2014 - 8:15 AM

Pagination for Processwire

Pagination for Processwire

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

?>

##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.