Skip to content

Instantly share code, notes, and snippets.

@wisnust
Forked from allenbell/pagination.js
Created February 16, 2021 16:42
Show Gist options
  • Save wisnust/f95e04df7880dba9bd71cadf932e3884 to your computer and use it in GitHub Desktop.
Save wisnust/f95e04df7880dba9bd71cadf932e3884 to your computer and use it in GitHub Desktop.
Ajax pagination for posts linked via ACF Relationships - JavaScript
jQuery(document).ready(function($){
$(document).on( 'click touchstart', '#pagination a', function( event ) {
// Fade out the old content for a smooth transition
$('#bike-features-inner').fadeOut();
// The pagination button you clicked will pass the page number back to the callback function
var page = $(this).attr('href');
// This will pass the post ID back to the callback function
var bikeID = $(this).closest('#pagination').find('#bike-id').attr('data-bike-id');
event.preventDefault();
$.ajax({
url: ajaxpagination.ajaxurl,
type: 'post',
data: {
action: 'dl_bike_features', // the name of the function in your php file
page: page, // access in php function via $_POST['page']
bikeID: bikeID // access in php function via $_POST['bikeID']
},
success: function( result ) {
// Replace the content of the container div w/ the output from dl_bikeFeatures()
$('#bike-features-inner').html(result);
// fade in this content for a smooth transition
$('#bike-features-inner').fadeIn();
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert('Sorry, something went wrong. Please try again.');
}
})
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment