Limit "Show More" Button to Logged In Users

Description/Instructions

This will allow you to limit how many times the load more button can be clicked to show more posts.

If this snippet helped, feel free to buy me a taco :)

Instructions

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
Copy Code

Let's Chat About this Snippet