tors Posted January 17 Posted January 17 I have custom code set up on a website I'm building (https://jamesfray.squarespace.com password: demo) so that my blogs have previous and next pagination show up at the bottom of the page. I'm using one blog (https://jamesfray.squarespace.com/blooming-detectives) as a list of books available in a series. I've reversed the order the posts show up in, so the oldest is first as that will be the first book in the series, but my previous/next are now the wrong way around. Can someone help me change the text so that where it currently says previous it will say next, and where it says next it will say previous please? Many thanks in advance!
Solution jpeter Posted January 18 Solution Posted January 18 (edited) The JavaScript below should work. It swaps the URL and Title. JavaScript (function(){ // Get previous element url and title const prevEl = document.querySelector('.item-pagination-link--prev'); const prevUrl = prevEl && prevEl.getAttribute('href'); const prevTitleEl = prevEl && prevEl.querySelector('.item-pagination-title'); const prevTitle = prevTitleEl && prevTitleEl.textContent; const prevTitleWrapper = prevEl && prevEl.querySelector('.pagination-title-wrapper'); const prevIcon = prevEl && prevEl.querySelector('.icon'); // Get next element url and title const nextEl = document.querySelector('.item-pagination-link--next'); const nextUrl = nextEl && nextEl.getAttribute('href'); const nextTitleEl = nextEl && nextEl.querySelector('.item-pagination-title'); const nextTitle = nextTitleEl && nextTitleEl.textContent; const nextTitleWrapper = nextEl && nextEl.querySelector('.pagination-title-wrapper'); const nextIcon = nextEl && nextEl.querySelector('.icon'); // Handle when user is on first page if(!prevEl && nextEl && nextTitleWrapper) { const tpl = document.createElement('template'); tpl.innerHTML = `<div class="item-pagination-icon icon icon--stroke"><svg class="caret-left-icon--small" viewBox="0 0 9 16"><polyline fill="none" stroke-miterlimit="10" points="7.3,14.7 2.5,8 7.3,1.2"></polyline></svg></div>`; nextTitleWrapper.innerHTML = nextTitleWrapper.innerHTML.replace(/\bNext\b/g, 'Previous'); nextEl.classList.add('item-pagination-link--prev'); nextEl.classList.remove('item-pagination-link--next'); nextIcon && nextEl.removeChild(nextIcon); nextIcon && nextEl.prepend(tpl.content) } // Handle when user is on last page if(prevEl && !nextEl && prevTitleWrapper) { const tpl = document.createElement('template'); tpl.innerHTML = `<div class="item-pagination-icon icon icon--stroke"><svg class="caret-right-icon--small" viewBox="0 0 9 16"><polyline fill="none" stroke-miterlimit="10" points="1.6,1.2 6.5,7.9 1.6,14.7"></polyline></svg></div>`; prevTitleWrapper.innerHTML = prevTitleWrapper.innerHTML.replace(/\bPrevious\b/g, 'Next'); prevEl.classList.add('item-pagination-link--next'); prevEl.classList.remove('item-pagination-link--prev'); prevIcon && prevEl.removeChild(prevIcon); prevIcon && prevEl.appendChild(tpl.content) } // Set next element title and url with the previous if(nextEl && nextTitleEl && prevUrl && prevTitle) { nextEl.setAttribute('href', prevUrl); nextTitleEl.textContent = prevTitle; } // Set previous element title and url with the next if(prevEl && prevTitleEl && nextUrl && nextTitle) { prevEl.setAttribute('href', nextUrl); prevTitleEl.textContent = nextTitle; } })() Make sure you add the JavaScript to the Footer using Code Injection and place it between <script> tags, e.g. <script> // Add JS here </script> Here's a video of me updating the JS via Chrome's dev tools to ensure it works. 9xlp5mzmcK.mp4 Edited January 18 by jpeter Add new code to account for first and last pages tors 1 Full stack developer who loves helping people out with anything web related. If you'd like to support me, buy me a coffee!
tors Posted January 18 Author Posted January 18 Thanks so much @jpeter, I really appreciate the help! It works perfectly mid-list but at the start and the end of the list the links break when clicked and look like below: This is book 3 of 3 and should say 'previous' with the name of book 2 and a link to book 2, instead this next link takes me to a 404 error. Is there any way to fix this please?
jpeter Posted January 18 Posted January 18 1 hour ago, tors said: ...the start and the end of the list the links break when clicked... @tors Updated the code above to account for the first and last books in the series. tors 1 Full stack developer who loves helping people out with anything web related. If you'd like to support me, buy me a coffee!
tors Posted January 18 Author Posted January 18 @jpeter thank you!! That works perfectly, really appreciate your help with this!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment