Skip to content

Instantly share code, notes, and snippets.

@JulioPotier
Created July 27, 2014 12:13
Show Gist options
  • Save JulioPotier/1207324668fc59255dee to your computer and use it in GitHub Desktop.
Save JulioPotier/1207324668fc59255dee to your computer and use it in GitHub Desktop.
add_action( 'wp_footer', 'infinite_load' );
function infinite_load() {
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
?>
<script>
jQuery(document).ready(function($){
if( window.history && history.pushState ) {
var $page = <?php echo $paged; ?>;
var $old_page = $page+1;
var $busy_infinite = false;
$('a.next').on('click', function(e){
var $actual = $(this).attr('href');
console.log( $actual );
e.preventDefault();
if( $busy_infinite == false ){
$busy_infinite = true;
$page++;
$actual = $actual.replace( 'page/'+$old_page, 'page/' + $page );
$('span.next').show();
$('a.next').hide();
$.ajax( {url: $actual + '/?nocache=1', headers:{'X-Requested-With':'BAWXMLHttpRequest'}} )
.done( function(data){
$busy_infinite = false;
if( data != '' ){
$('span.next').hide();
$('a.next').show();
$('#content').append( data );
window.history.pushState(window.document.title, 'foobar', $actual );
}
})
.fail( function() {
$('span.next').hide();
});
}
});
}
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment