facetwp infinite scroll

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php

/** adds a scroll handler to turn load more into a infinite scroll **/

add_action( ‘wp_footer’, function() {
?>
<script>
(function($) {
if (‘object’ !== typeof FWP) {
return;
}
$(function() {
/** scroll */
function scrollhander() {
var pos = $(window).height() – $(‘.fwp-load-more’).offset().top + $(window).scrollTop();
if ( pos > 0 && !FWP.is_load_more ) {
$( window ).off( ‘scroll’, scrollhander );
$(‘.fwp-load-more’).trigger(‘click’);
}
};

function addhandler() {
if ( FWP.settings.pager.page < FWP.settings.pager.total_pages ) {
$( window ).on( ‘scroll’, scrollhander );
} else {
$( window ).off( ‘scroll’, scrollhander );
}
}

addhandler();

/** re-add handler after facetwp reloads */
FWP.hooks.addAction( ‘facetwp/loaded’, function() { addhandler() } );
});
})(jQuery)
</script>
<?php
}, 100 );
{{/gist}}