function centil_infinite_scroll($pid){
if (is_single()) { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
$(window).scroll(function() {
var footerPos = $('footer').last().position().top;
var pos = $(window).scrollTop();
// Load Previous article
if (pos+(screen.height*1) > footerPos) {
if ($(".centil-infinite-scroll").first().hasClass('working')) {
return false;
} else {
$(".centil-infinite-scroll").first().addClass('working');
}
var centilPreviousPostId = $(".centil-infinite-scroll").first().text();
var data = {
'action': 'centil_is',
'centilPreviousPostId': centilPreviousPostId
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post('<?php echo admin_url("admin-ajax.php"); ?>', data, function(response) {
$(".centil-infinite-scroll").first().replaceWith(response);
}, 'html');
}
// Update new URL
var currUrl = $(".centil-post-header").first().attr("url");
var currTitle = $(".centil-post-header").first().attr("title");
if ($(".centil-post-header").length > 1 && history.pushState) {
for (var i=0; i<$(".centil-post-header").length; i++) {
if (pos+(screen.height/2) >= $(".centil-post-header").eq(i).next().position().top) {
currUrl = $(".centil-post-header").eq(i).attr("url");
currTitle = $(".centil-post-header").eq(i).attr("title");
}
}
}
if (location.href != currUrl) {
history.pushState({}, currTitle, currUrl);
}
});
});
</script>
<?php }
}
add_action( 'wp_head', 'centil_infinite_scroll' );
function centil_infinite_scroll_callback() {
if (isset($_POST['centilPreviousPostId']) && $_POST['centilPreviousPostId']) {
// The Query
$the_query = new WP_Query(array('p'=>$_POST['centilPreviousPostId']));
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'template-parts/content-news-infinity', get_post_type() );
}
}
/* Restore original Post Data */
wp_reset_postdata();
}
wp_die();
}
add_action( 'wp_ajax_centil_is', 'centil_infinite_scroll_callback' );
add_action( 'wp_ajax_nopriv_centil_is', 'centil_infinite_scroll_callback' );