• Resolved aneemal

    (@aneemal)


    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);
             }
          });
       });

    This is 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 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.

    Can you help?!…

Viewing 1 replies (of 1 total)
  • Thread Starter aneemal

    (@aneemal)

    Aha, I think my syntax was wrong.

    I changed:

    action: 'do_ajax',
    data: {
    'post_id' : '72'
    },

    to:

    data: {
    'action' : 'do_ajax',
    'post_id' : '72'
    },

    and I’m getting the correct response from the console.log.

    So now I’m figuring out how best to handle the array 🙂

Viewing 1 replies (of 1 total)
  • The topic ‘Retrieve and loop through comments with AJAX and jQuery’ is closed to new replies.