I’m working on a custom theme, that is basically a one-page blog, with posts being called inside a slider. The slider navigation works using pagination, but with links displaying like a timeline and showing the date of each post.
I want to display and manipulate the comments for each post outside the slider, so I’m trying to load them using ajax and a jQuery click function.
This is the jQuery click function, that at the moment resides in my header:
$(".timeline-entry a").on('click', function(event) {
event.preventDefault();
var myComments = '<?php echo json_encode(get_comments('post_id=72')); ?>'; //for testing with a hard-coded post number...
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
action: 'do_ajax',
data: {
'post_id' : '72'
},
dataType: 'json',
success: function(myResult) {
//$.each (myResult, function() {
//alert(myResult.comment_author);
//}); Can't seem to retrieve any comments or data at all
alert("sOmethingishappenin'"); //This is alerted, so the AJAX is working?!
},
error: function(error) {
alert(error);
}
});
});
I put this line in for testing var myComments = '<?php echo json_encode(get_comments('post_id=72')); ?>'; and the array that is returned looks like this:
[{"comment_ID":"2","comment_post_ID":"72","comment_author":"Mr WordPress","comment_author_email":"","comment_author_url":"http:\/\/wordpress.org\/","comment_author_IP":"","comment_date":"2010-07-11 12:10:08","comment_date_gmt":"2010-07-11 12:10:08","comment_content":"Hi, this is a comment.<br \/>To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.","comment_karma":"0","comment_approved":"1","comment_agent":"","comment_type":"","comment_parent":"0","user_id":"0"}
But using the AJAX function, I can’t seem to grab any of this information. So far, I can only get a string to show as an alert. If I try anything related to myResult I only get 0.
(@aneemal)
11 years, 7 months ago
I’m working on a custom theme, that is basically a one-page blog, with posts being called inside a slider. The slider navigation works using pagination, but with links displaying like a timeline and showing the date of each post.
I want to display and manipulate the comments for each post outside the slider, so I’m trying to load them using ajax and a jQuery click function.
This is the jQuery click function, that at the moment resides in my header:
This is in my functions.php:
I put this line in for testing
var myComments = '<?php echo json_encode(get_comments('post_id=72')); ?>';
and the array that is returned looks like this:[{"comment_ID":"2","comment_post_ID":"72","comment_author":"Mr WordPress","comment_author_email":"","comment_author_url":"http:\/\/wordpress.org\/","comment_author_IP":"","comment_date":"2010-07-11 12:10:08","comment_date_gmt":"2010-07-11 12:10:08","comment_content":"Hi, this is a comment.<br \/>To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.","comment_karma":"0","comment_approved":"1","comment_agent":"","comment_type":"","comment_parent":"0","user_id":"0"}
But using the AJAX function, I can’t seem to grab any of this information. So far, I can only get a string to show as an alert. If I try anything related to
myResult
I only get0
.Can you help?!…