Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipelavinz/9382151 to your computer and use it in GitHub Desktop.
Save felipelavinz/9382151 to your computer and use it in GitHub Desktop.
Ejemplo de utilización de la acción wp_ajax para hacer peticiones por AJAX a WordPress
jQuery(document).ready(function($){
var postTemplate = function( post ){
return '<article class="hentry"><a href="#post-'+ post.ID +'">'+ post.post_title +'</a></article>';
}
$('#load-next-posts').on('click', function(){
// la variable ajaxurl debe estar definida y apuntar a wp-admin/admin-ajax.php
// en la data enviada con la petición, el parámetro "action" debe coincidir con la detección de la acción en PHP
$.get( ajaxurl, {
action: 'get_next_posts',
offset: $('.hfeed').find('.hentry').length
}, function(data){
var q = data.length;
if ( q > 0 ) {
for ( var i = 0; i < q; i++ ){
$('.hfeed').append( postTemplate(data[i]) );
}
} else {
alert('Ya no hay más posts!')
}
}, 'json');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment