Last active
July 12, 2018 12:52
-
-
Save adamtomat/247eb50f06f4c718aa25e1b4e4a85346 to your computer and use it in GitHub Desktop.
Lumberjack Pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\PostTypes; | |
use Rareloop\Lumberjack\QueryBuilder\Post as LumberjackPost; | |
class Post extends LumberjackPost | |
{ | |
public static function paginate($perPage = 10) | |
{ | |
global $paged; | |
if (!isset($paged) || !$paged) { | |
$paged = 1; | |
} | |
$args = [ | |
'posts_per_page' => $perPage, | |
'paged' => $paged | |
]; | |
// Pagination requires wordpress's query_posts method instead of Timber's. | |
query_posts($args); | |
return static::query($args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Timber\Timber; | |
use App\PostTypes\Post; | |
use Rareloop\Lumberjack\QueryBuilder\QueryBuilder as LumberjackQueryBuilder; | |
class QueryBuilder extends LumberjackQueryBuilder | |
{ | |
private $searchTerm; | |
private $page; | |
protected $postClass = Post::class; | |
public function getParameters() | |
{ | |
$params = parent::getParameters(); | |
if (isset($this->page)) { | |
$params['paged'] = (int)$this->page; | |
} | |
return $params; | |
} | |
/** | |
* Use this instead of get() | |
*/ | |
public function paginate($perPage = 10, $page = null) | |
{ | |
global $paged; | |
if (isset($page)) { | |
$paged = $page; | |
} | |
if (!isset($paged) || !$paged) { | |
$paged = 1; | |
} | |
$this->limit($perPage); | |
$this->page($paged); | |
query_posts($this->getParameters()); | |
return $this->get(); | |
} | |
public function page($page) | |
{ | |
$this->page = $page; | |
return $this; | |
} | |
public static function getPagination($prefs = []) | |
{ | |
return Timber::get_pagination($prefs); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\ViewModels; | |
use App\QueryBuilder; | |
use Rareloop\Lumberjack\ViewModel; | |
class Pagination extends ViewModel | |
{ | |
public static function fromQueryBuilder($resultsPerPage, $forPage) | |
{ | |
$pagination = QueryBuilder::getPagination(); | |
return new static( | |
$resultsPerPage, | |
$forPage, | |
$pagination['current'], | |
$pagination['total'], | |
$pagination['pages'], | |
$pagination['prev'], | |
$pagination['next'] | |
); | |
} | |
public function __construct($resultsPerPage, $forPage, $current, $total, $pages = [], $prev = [], $next = []) | |
{ | |
$this->pagination = [ | |
'current' => $current, | |
'total' => $total, | |
'pages' => $pages, | |
'next' => $next, | |
'prev' => $prev, | |
]; | |
} | |
public function toArray() : array | |
{ | |
return $this->pagination; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Template Name: News | |
* The template for displaying all news articles. | |
* | |
*/ | |
namespace App; | |
use Timber\Timber; | |
use App\PostTypes\Post; | |
use Rareloop\Lumberjack\Page; | |
use App\ViewModels\Breadcrumbs; | |
use App\ViewModels\NewsEventsTeasers; | |
use App\Http\Responses\TimberResponse; | |
use App\ViewModels\Pagination; | |
class PageNewsController | |
{ | |
private $page; | |
private $resultsPerPage = 10; | |
public function handle() | |
{ | |
$this->page = new Page(); | |
$context = Timber::get_context(); | |
$page = $_GET['page'] ?? 1; | |
$context['news'] = (new NewsEventsTeasers(Post::paginate($this->resultsPerPage))); | |
$context['pagination'] = Pagination::fromQueryBuilder($this->resultsPerPage, $page); | |
return new TimberResponse('templates/news', $context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment