Skip to content

Instantly share code, notes, and snippets.

@ashby
Created May 1, 2014 00:39
Show Gist options
  • Select an option

  • Save ashby/11442218 to your computer and use it in GitHub Desktop.

Select an option

Save ashby/11442218 to your computer and use it in GitHub Desktop.
JS: WP AJAX
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j('#load-more').on('click', function(e){
var $this = $j(this);
e.preventDefault();
$j('#results-message').css('display', 'block');
post_number = $this.data('post-number');
url = $this.data('url');
pag = $this.data('page');
media = $this.data('media');
var data = {
action: 'get_more_posts',
post_number: post_number,
media: media,
pag: pag
}
$j.ajax({
cache: false,
dataType : 'json',
url: url + '/wp-admin/admin-ajax.php',
type: 'POST',
data: data,
error: function(response, textStatus, errorThrown) {
// console.log(response);
// console.log(response.html);
},
success: function(response, textStatus, jqXHR) {
if (response.status == 'success') {
$j('#results').append(response.html);
$j('#results-message').css('display', 'none');
if( response.none_found){
$j('#results-message').text('No More Posts.');
}else{
if(!response.have_more){
$this.css('display', 'none');
$j('.back-to-top').show();
}else if(response.have_more){
pag++;
$j('#load-more').data('page', pag);
}
}
}
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment