Hey everyone. I’m currently trying to implement a next/previous button at the bottom of my post page. I am using this code from this tutorial: How to add Next/Previous buttons to your post (wix-blog-community.com)
import wixData from 'wix-data';
import wixLocation from 'wix-location';
let posts = [];
$w.onReady(function () {
wixLocation.onChange((location) => {
init();
})
init();
});
async function getPosts() {
return wixData.query("Blog/Posts")
.limit(100)
.ascending("publishedDate")
.find()
.then((results) => {
posts = results.items;
})
}
function getCurrentPostIndex(url) {
let index;
posts.forEach((post, i) => {
if (post.postPageUrl === url) {
index = i;
}
});
return index;
}
function init() {
console.log('init');
let currentPostUrl;
let currentPostIndex;
wixData.query("Blog/Posts")
.limit(100)
.ascending("publishedDate")
.find()
.then((results) => {
posts = results.items;
}).then(() => {
currentPostUrl = `/${wixLocation.path.join('/')}`;
currentPostIndex = getCurrentPostIndex(currentPostUrl) || 0;
console.log(currentPostIndex);
if (currentPostIndex === 0) {
$w('#previousBox').hide();
} else {
$w('#previousBox').show();
const prevPostTitle = posts[currentPostIndex - 1].title;
$w('#previousPost').text = prevPostTitle;
}
if (currentPostIndex === posts.length) {
$w('#nextBox').hide();
} else {
$w('#nextBox').show();
const nextPostTitle = posts[(currentPostIndex || 0) + 1].title;
$w('#nextPost').text = nextPostTitle;
}
})
$w('#previous').onClick(() => {
wixLocation.to(posts[currentPostIndex - 1].postPageUrl);
});
$w('#previousPost').onClick(() => {
wixLocation.to(posts[currentPostIndex - 1].postPageUrl);
});
$w('#next').onClick(() => {
wixLocation.to(posts[currentPostIndex + 1].postPageUrl);
});
$w('#nextPost').onClick(() => {
wixLocation.to(posts[currentPostIndex + 1].postPageUrl);
});
}
My issue is that I have more than 100 blog posts. I’m aware that creating my own collection can allow me a limit of 1000, but I don’t know how to implement it in this code. I’m a complete beginner at coding. Any help and guidance for a solution will be appreciated.