Skip to content

Commit

Permalink
1.2.16: Added nextPost and previousPost to the blog post component.
Browse files Browse the repository at this point in the history
Fixes #80
Fixes #284
  • Loading branch information
daftspunk committed Mar 20, 2017
1 parent ea9db64 commit c5337d5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
35 changes: 34 additions & 1 deletion components/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,44 @@ protected function loadPost()
* Add a "url" helper attribute for linking to each category
*/
if ($post && $post->categories->count()) {
$post->categories->each(function($category){
$post->categories->each(function($category) {
$category->setUrl($this->categoryPage, $this->controller);
});
}

return $post;
}

public function previousPost()
{
return $this->getPostSibling(-1);
}

public function nextPost()
{
return $this->getPostSibling(1);
}

protected function getPostSibling($direction = 1)
{
if (!$this->post) {
return;
}

$method = $direction === -1 ? 'previousPost' : 'nextPost';

if (!$post = $this->post->$method()) {
return;
}

$postPage = $this->getPage()->getBaseFileName();

$post->setUrl($postPage, $this->controller);

$post->categories->each(function($category) {
$category->setUrl($this->categoryPage, $this->controller);
});

return $post;
}
}
30 changes: 30 additions & 0 deletions models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,36 @@ public function getSummaryAttribute()
return Html::limit($this->content_html, 600);
}

//
// Next / Previous
//

/**
* Returns the next post, if available.
* @return self
*/
public function nextPost()
{
return self::isPublished()
->where('id', '>' , $this->id)
->orderBy('id', 'asc')
->first()
;
}

/**
* Returns the previous post, if available.
* @return self
*/
public function previousPost()
{
return self::isPublished()
->where('id', '<' , $this->id)
->orderBy('id', 'desc')
->first()
;
}

//
// Menu helpers
//
Expand Down
1 change: 1 addition & 0 deletions updates/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@
1.2.13: Improved support for Static Pages menus, added a blog post and all blog posts.
1.2.14: Added post exception property to the post list component, useful for showing related posts.
1.2.15: Back-end navigation sort order updated.
1.2.16: Added `nextPost` and `previousPost` to the blog post component.

0 comments on commit c5337d5

Please sign in to comment.