Skip to content

Instantly share code, notes, and snippets.

@allenbell
Created February 10, 2016 20:14
Show Gist options
  • Save allenbell/c43c9e84d5b63b9f2af8 to your computer and use it in GitHub Desktop.
Save allenbell/c43c9e84d5b63b9f2af8 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