Javascript Wordpress-get\u comment\u author()返回匿名

Javascript Wordpress-get\u comment\u author()返回匿名,javascript,php,wordpress,Javascript,Php,Wordpress,我开发了一个wordpress插件,在网站的每个页面的头部插入一个JS脚本。我试图将几个变量传递给脚本(在PHP中),例如,当有评论时,评论作者的姓名、电子邮件和网站 我曾尝试使用get_comment_author()、get_comment_author_url()和get_comment_author_email(),但它总是返回“匿名”,即使我在发布评论时只输入了名称、网站和邮件地址 代码如下: add_action('wp_head', 'insert_script'); funct

我开发了一个wordpress插件,在网站的每个页面的头部插入一个JS脚本。我试图将几个变量传递给脚本(在PHP中),例如,当有评论时,评论作者的姓名、电子邮件和网站

我曾尝试使用get_comment_author()、get_comment_author_url()和get_comment_author_email(),但它总是返回“匿名”,即使我在发布评论时只输入了名称、网站和邮件地址

代码如下:

add_action('wp_head', 'insert_script');

function insert_script(){


  $name = get_comment_author();
  $website = get_comment_author_url();
  $email = get_comment_author_email();



    echo " <script type='text/javascript'>

                var _gigo= _gigo || {};
                _gigo['firstname'] = '' ;
                _gigo['lastname'] = '".$name."' ;
                _gigo['company'] = '".$website."' ;
                _gigo['email'] = '".$email."' ;
           </script>";
}
add_action('wp_head','insert_script');
函数插入_脚本(){
$name=get_comment_author();
$WEBITE=get_comment_author_url();
$email=get_comment_author_email();
回声“
var_gigo=_gigo | |{};
_gigo['firstname']='';
_gigo['lastname']='“$name.”;
_gigo['company']='“$website.”;
_gigo['email']='“$email.”;
";
}
你知道函数返回匿名作者的原因吗?我如何修复它? 提前感谢。

如第页所示,您需要为该函数提供一个ID,或者它需要位于循环中。因为在您的情况下两者都不是真的,所以它返回
anonymous

描述 检索当前注释的作者。如果注释的“注释作者”字段为空,则假定为“匿名”人。这个函数应该位于WordPress循环中

用法

要通过post获取评论,可以使用该函数。您可以按如下方式使用它:

<?php
global $post; // get the current post
$postId = $post->ID; // get the current post ID
$comments = get_comments('post_id=' . $postId); // This gets all comments from current post
?>

您可以检查如何使用下面的输出


如果还不清楚,请告诉我。

嗨@Sander Koedood,谢谢你的回答。如何检索注释ID?我看到有一个函数,get_comment_ID(),但“靠自己”似乎不起作用。例如,一个用户留下一条评论,给出他的姓名、电子邮件和地址,我如何检索他刚刚发布的评论的ID?我必须查看代码才能回答这个问题。下面是整个插件的要点:我已经为你更新了我的答案。我希望你能这样解决。
<?php
global $post; // get the current post
$postId = $post->ID; // get the current post ID
$comments = get_comments('post_id=' . $postId); // This gets all comments from current post
?>