Skip to content

Instantly share code, notes, and snippets.

@jyek
Last active November 8, 2021 09:38
Show Gist options
  • Save jyek/5141bc6166b01419d43f to your computer and use it in GitHub Desktop.
Save jyek/5141bc6166b01419d43f to your computer and use it in GitHub Desktop.
Ghost Blog - Next Prev Posts
/*
* Ghost Blog: Next & Previous Posts Workaround
*/
// Step 1: Insert at end of default.hbs. Requires jQuery.
$(function(){
var NextPrevLinksModule = function(){
var curr,
$prevLink,
$nextLink;
return {
init: function(){
curr = $('#curr-post-uuid').html();
$prevLink = $('.prev-post');
$nextLink = $('.next-post');
$prevLink.hide();
$nextLink.hide();
this.parseRss();
},
// creates previous and next links
createLinks: function(items){
for (var i = 0; i < items.length; i++){
var uuid = $(items[i]).find('guid').text();
if (uuid === curr){
if (i < items.length-1){
$prevLink.attr('href', $(items[i+1]).find('link').text());
$prevLink.show();
}
if (i > 0){
$nextLink.attr('href', $(items[i-1]).find('link').text());
$nextLink.show();
}
}
}
},
// iteratively parses rss feeds to generate posts object
parseRss: function(page, items, lastId){
self = this;
page = page || 1;
items = items || undefined;
lastId = lastId || '';
$.get('/rss/' + page, function(data){
var posts = $(data).find('item');
var currId;
if (posts.length > 0) currId = $(posts[0]).find('guid').text();
if (currId === lastId){
// if this page has already been parsed, create links
self.createLinks(items);
} else {
// if not, continue parsing posts
items = items ? items.add(posts) : posts;
lastId = currId;
self.parseRss(page+1, items, lastId);
}
});
}
};
}();
NextPrevLinksModule.init();
});
// Step 2: Insert into post.hbs
<span id="curr-post-uuid" style="visibility: hidden; display: none;">{{uuid}}</span>
<a class="prev-post">Previous Post</a>
<a class="next-post">Next Post</a>
@acroyear
Copy link

acroyear commented Oct 7, 2014

this worked for me perfectly fine. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment