Created
December 1, 2023 10:59
-
-
Save Asikur22/0d107fc2d6db6e7dc66d98e14e8298c8 to your computer and use it in GitHub Desktop.
WordPress Load Next Post with AJAX
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
add_action( 'wp_footer', function () { | |
if ( is_single() ) { | |
?> | |
<script> | |
jQuery( function ( $ ) { | |
var gl_load_spinner = $( '#gl-load-more-spinner' ); | |
var gl_next_post_link = $( '.elementor-post-navigation__next.elementor-post-navigation__link a' ).attr( 'href' ); | |
var canBeLoaded = true; | |
const bottomOffset = 2000; | |
gl_load_spinner.hide(); | |
$( '.elementor-element.elementor-widget.elementor-widget-post-navigation' ).hide(); | |
$( window ).scroll( function () { | |
if ( $( document ).scrollTop() > ( $( document ).height() - bottomOffset ) && canBeLoaded == true ) { | |
$.ajax( { | |
url: gl_next_post_link, | |
dataType: 'html', | |
type: 'GET', | |
beforeSend: function () { | |
gl_load_spinner.show(); | |
canBeLoaded = false; | |
}, | |
success: function ( data ) { | |
let html = $( data ).find( '#main' ); | |
if ( html ) { | |
gl_next_post_link = html.find( '.elementor-post-navigation__next.elementor-post-navigation__link a' ).attr( 'href' ); | |
html.find( '#gl-load-more-spinner' ).closest( '.elementor-section.elementor-top-section.elementor-element' ).remove(); | |
gl_load_spinner.before( html.html() ); | |
gl_load_spinner.hide(); | |
if ( gl_next_post_link ) { | |
canBeLoaded = true; | |
} | |
} | |
} | |
} ); | |
} | |
} ); | |
} ); | |
</script> | |
<?php | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment