Wordpress - get_comment_author() 返回匿名 [英] Wordpress - get_comment_author() returns anonymous

查看:15
本文介绍了Wordpress - get_comment_author() 返回匿名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

代码如下:

add_action('wp_head', '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['名字'] = '';_gigo['lastname'] = '".$name."' ;_gigo['company'] = '".$website."' ;_gigo['email'] = '".$email."' ;";}

你知道为什么这些函数会返回一个匿名作者以及我该如何修复它吗?提前致谢.

解决方案

可以在 this 上看到

a> 页面,您需要为该函数提供一个 ID,或者它需要在循环内.因为在您的情况下都不是真的,它返回 anonymous.

<块引用>

说明检索当前评论的作者.如果评论有一个空的 comment_author 字段,则假定为匿名"人.此函数旨在存在于 WordPress 循环中.

用法<?php $author = get_comment_author( $comment_ID );?>

<小时>

要通过帖子获取评论,您可以使用 get_comments() 函数.您可以按如下方式使用它:

ID;//获取当前帖子ID$comments = get_comments('post_id=' . $postId);//获取当前帖子的所有评论?>

您可以查看链接,了解如何使用后面的输出.

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

I have developed a wordpress plugin that inserts a JS script in the head of every page of the site. I am trying to pass a couple variables to the script (in PHP) such as the name, the email, and the website of the author of the comment, when there's a comment.

I have tried to use the get_comment_author(), get_comment_author_url() and get_comment_author_email() but it always returns "Anonymous", even if I just entered a name, website and mail adress when posting a comment.

Here is the code:

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

Do you know why the functions return an anonymous author and how I could fix it ? Thanks in advance.

解决方案

As can be seen on this page, you need to supply an ID to that function, or it needs to be inside the loop. Because neither is true in your case, it returns anonymous.

Description Retrieve the author of the current comment. If the comment has an empty comment_author field, then 'Anonymous' person is assumed. This function is meant to live inside of the WordPress loop.

Usage

<?php $author = get_comment_author( $comment_ID ); ?>


To get comments by post, you can use the get_comments() function. You could use it as follows:

<?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
?>

You can check the link on how you can use the output that follows.

If this is still unclear, let me know.

这篇关于Wordpress - get_comment_author() 返回匿名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆