Last active
August 29, 2015 13:57
-
-
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
This file contains 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
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