diff --git a/liquet/functions.php b/liquet/functions.php index a10c1d5..dc83dc7 100644 --- a/liquet/functions.php +++ b/liquet/functions.php @@ -24,18 +24,17 @@ add_action( 'wp_enqueue_scripts', function () { function lazy_load_ajax_handler() { - $args = json_decode( stripslashes( $_POST['query'] ), true ); - $args['paged'] = $_POST['page'] + 1; - $args['post_status'] = 'publish'; - query_posts( $args ); + query_posts( [ + 'post_status' => 'publish', + 'paged' => $_POST['page'] + 1, + 'name' => $_POST['name'], + ] ); - if ( have_posts() ) { - while ( have_posts() ) { - the_post(); - get_template_part( 'post-preview', get_post_format() ); - } + while ( have_posts() ) { + the_post(); + get_template_part( 'post-preview', get_post_format() ); } - die; + wp_die(); } add_action( 'wp_ajax_lazy_load', 'lazy_load_ajax_handler' ); diff --git a/liquet/header.php b/liquet/header.php index b874ef7..e34b280 100644 --- a/liquet/header.php +++ b/liquet/header.php @@ -11,10 +11,9 @@ diff --git a/src/js/lazy.js b/src/js/lazy.js index 78b5713..84e2d8c 100644 --- a/src/js/lazy.js +++ b/src/js/lazy.js @@ -4,6 +4,7 @@ export default () => { const bottomOffset = 300 const app = document.getElementById('app') let loading = false + let page = WPParams.lazy.current_page function load() { @@ -11,18 +12,21 @@ export default () => { if (!loading && pixelToBottom < bottomOffset) { loading = true + $.ajax({ url: WPParams.lazy.ajaxurl, data: { - 'action': 'lazy_load', - 'query': JSON.stringify(WPParams.lazy.posts), - 'page': WPParams.lazy.current_page + name: window.location.pathname + .replace(/(\/\d+){3}\//, '') + .replace(/\/$/, ''), + action: 'lazy_load', + page, }, type: 'POST', - success: function (data) { + success: (data) => { if (data) { $('#list').find('hr:last-of-type').after(data) // where to insert posts - WPParams.lazy.current_page++ + page++ loading = false } }