I'm working on a theme that uses a slider to display blog posts. Beneath the slider is a pagination that displays like a timeline, with a date instead of a page number, calling the corresponding blog post. I want to display the comments for each post, but outside the slider. So, I have this code in my header, to get the comments with ajax:
:: EDITED (now using wp-admin/admin-ajax.php to handle request, as advised below) ::
$(".timeline-entry a").live('click', function(event) {
event.preventDefault();
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
action: 'do_ajax',
data: {
'post_id' : '72' //using a post id that I *know* has comments, for testing!
},
dataType: "json",
success: function(myResult) {
alert(myResult);
},
error: function(error) {
alert(error);
}
});
});
In my functions.php:
add_action('wp_ajax_nopriv_do_ajax', 'retrieve_comments');
add_action('wp_ajax_do_ajax', 'retrieve_comments');
function retrieve_comments(){
$myPost = $_REQUEST['post_id'];
$output = get_comments('post_id=' + $myPost);
$output = json_encode($output);
if(is_array($output)){
print_r($output);
}
else{
echo $output;
}
die;
};
I get 0
in the alert box... so that means the ajax is happening, but something is wrong with my php function, right?
Thanks for your help.
.on( 'click', function( e ) { ...
. The.live()
function is replaced.