Last active
February 3, 2022 23:17
-
-
Save mattradford/405ddc8577fec11b88f5d58e22d42b47 to your computer and use it in GitHub Desktop.
FacetWP load more and load all
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
// Adapted from https://gist.github.com/mgibbs189/f2469009a7039159e229efe5a01dab23 | |
function fwp_load_more() { | |
?> | |
<script> | |
(function($) { | |
$(function() { | |
if ('object' != typeof FWP) { | |
return; | |
} | |
wp.hooks.addFilter('facetwp/template_html', function(resp, params) { | |
if (FWP.is_load_more) { | |
FWP.is_load_more = false; | |
$('.facetwp-template').append(params.html); | |
return true; | |
} | |
return resp; | |
}); | |
$(document).on('click', '.fwp-load-more', function() { | |
$('.fwp-load-more').html('Loading more products'); | |
$('.fwp-load-more').after('<span class="fwp-loader"></span>'); | |
FWP.is_load_more = true; | |
FWP.paged = parseInt(FWP.settings.pager.page) + 1; | |
FWP.soft_refresh = true; | |
FWP.refresh(); | |
}); | |
$(document).on('click', '.fwp-load-all', function() { | |
$('.fwp-load-all').html('Loading all products'); | |
$('.fwp-load-all').after('<span class="fwp-loader"></span>'); | |
FWP.soft_refresh = true; | |
FWP.extras.per_page = 500 | |
FWP.refresh(); | |
}); | |
$(document).on('facetwp-loaded', function() { | |
$('.fwp-loader').hide(); | |
if (FWP.settings.pager.page < FWP.settings.pager.total_pages) { | |
if (! FWP.loaded && 1 > $('.fwp-load-more').length) { | |
$('.facetwp-template').after('<div class="facetwp__loader"><button class="fwp-load-more jbutton">Show more</button><button class="fwp-load-all jbutton">Show all</button></div>'); | |
} | |
else { | |
$('.fwp-load-more').html('Show more').show(); | |
} | |
} | |
else { | |
$('.fwp-load-more').hide(); | |
$('.fwp-load-all').hide(); | |
} | |
}); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
add_action( 'wp_head', 'fwp_load_more', 99 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment