使用AJAX在点击时加载每个帖子的评论

时间:2012-09-10 作者:Amal

我正在开发一个使用滑块显示博客帖子的主题。滑块下方是一个分页,显示方式类似于时间线,用日期而不是页码,调用相应的博客文章。我想显示每个帖子的评论,但在滑块之外。因此,我的标题中有以下代码,用于使用ajax获取注释:

::已编辑(现在使用wp-admin/admin-ajax.php 处理请求,如下所示):

$(".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);
     }
  });
});
在我的功能中。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;
}; 
我明白了0 在警报框中。。。这意味着ajax正在发生,但我的php函数有问题,对吗?

谢谢你的帮助。

2 个回复
最合适的回答,由SO网友:Amal 整理而成

啊哈,我想我的语法错了。

我更改了:

 action: \'do_ajax\',
 data: {
    \'post_id\' : \'72\' //using a post id that I *know* has comments, for testing!
 },
收件人:

data: {
    \'action\' : \'do_ajax\',
    \'post_id\' : \'72\'
},
我从console.log 这表明:

[{"comment_ID":"1","comment_post_ID":"1","comment_author":"Mr WordPress","comment_author_email":"","comment_author_url":"http:\\/\\/wordpress.org\\/","comment_author_IP":"","comment_date":"2012-08-28 19:55:20","comment_date_gmt":"2012-08-28 19:55:20","comment_content":"Hi, this is a comment.<br \\/>To delete a comment, just log in and view the post&#039;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"},{"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&#039;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"}
因此,现在我正在研究如何最好地处理阵列:)

SO网友:Milo

您的PHP文件没有在WordPress环境的上下文中加载,因此您无法访问任何WordPress函数。看见AJAX in Plugins 在Codex中,了解如何正确处理WordPress中的AJAX调用,尤其是Ajax on the Viewer-Facing Side. 尽管页面的标题不同,但这也是在主题中处理AJAX的方式。

编辑-This is a good tutorial (链接在上述Codex页面的底部)了解WordPress AJAX功能。

结束

相关推荐

将灵活的旋转木马整合到WordPress:如何编写ajax_allback函数

json对我来说是全新的,如何为agile carousel 在wordpress工作?下面是我需要编写为php数组的jason数据格式,因为它可以由$解析。getJSON()函数。 [{ \"content\": \"<div class=\'slide_inner\'><a class=\'photo_link\' href=\'#\'><img class=\'photo\' src=\'slides/slide_3.png\' alt=\'Bike