Created
May 25, 2020 01:43
-
-
Save LionRoar/718bb5e6d8afab0a013ddc25f5efa1ac to your computer and use it in GitHub Desktop.
Wordpress Ajax load posts js file
This file contains hidden or 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 | |
| }; | |
| $('.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