Created
May 25, 2020 01:45
-
-
Save LionRoar/a0b0b96f4d313974a6fc118c7732b4ee to your computer and use it in GitHub Desktop.
Wordpress Ajax Load more all
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(function($){ | |
var ajaxurl = load_more_posts_access.ajaxurl; // from localized script | |
var page = 2; //number of next page | |
var loadMore = function(){ | |
if($(window).scrollTop() == $(document).height() - $(window).height()){ // if reached the end of the page | |
var data = { | |
'action':'load_more_posts', // ajax action | |
'paged': page,//page number | |
'security': load_more_posts_access.security,//security | |
'request_uri': window.location.pathname | |
}; | |
$('.load').show(); // show loader | |
$.post(ajaxurl, data,function(response){ | |
var $res; | |
// console.log(response) | |
if(response.data) //if there is posts | |
$res = response.data; | |
else { | |
$res = '<div><code>you have reached the end, want to see more subscribe to the newsletter</code></div>'; | |
$(window).off('scroll',loadMore); | |
} | |
$load = $(`<div class="load-${page}"></div>`); | |
$load.appendTo('.articles-list').hide(); | |
$load.append($res).fadeIn(500); | |
page++; // next page number | |
$('.load').hide(); // hide loader | |
}); | |
} | |
}; | |
$(window).scroll(loadMore); // add event on window scroll | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment