Snippets / JS
JS
Limit "Show More" Button to Logged In Users
D
Donald McGuinn
·
223 views
·
Updated 2 years ago
This will allow you to limit how many times the load more button can be clicked to show more posts.
Change if (clickCount > 1) { to however many clicks you want.
Change window.location.href = 'https://voxel.guide/login-registration/'; to your login URL.
<script>
document.addEventListener('DOMContentLoaded', function () {
// Check if the user is logged in
// This checks for a specific class on the body tag which is common in WordPress
const userLoggedIn = document.body.classList.contains('logged-in');
if (!userLoggedIn) {
const loadMoreButton = document.querySelector('.ts-load-more');
const storageKey = 'loadMoreClickCount';
let clickCount = parseInt(localStorage.getItem(storageKey)) || 0;
loadMoreButton.addEventListener('click', function () {
clickCount++;
localStorage.setItem(storageKey, clickCount);
if (clickCount > 1) {
window.location.href = 'https://voxel.guide/login-registration/';
}
});
}
});
</script>
JS · 22 lines
Discussion
Ask a question or share how you used this.
Log in
to join the discussion.
No comments yet — be the first.