如何显示“自定义昵称"在 Wordpress 评论上? [英] How to display the "custom nickname" on Wordpress Comments?

查看:34
本文介绍了如何显示“自定义昵称"在 Wordpress 评论上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发表评论时,它总是打印在用户 CMS 区域设置的不可编辑昵称.我想打印自定义昵称(可以通过下拉列表选择的昵称).

When I post a comment, it prints always the non-editable nickname set on the Users CMS area. I'd like to print the custom nickname (the one that can be chosed by dropdown list).

我实际上使用了 comment_author_link(); 并且似乎还不够.我应该怎么用?

I actually use comment_author_link(); and seems it is not enough. How can I should use?

推荐答案

WordPress 使用以下代码检索当前评论的作者:

WordPress uses the following code to retrieve the author of the current comment:

/**
 * Retrieve the author of the current comment.
 *
 * If the comment has an empty comment_author field, then 'Anonymous' person is
 * assumed.
 *
 * @since 1.5.0
 * @uses apply_filters() Calls 'get_comment_author' hook on the comment author
 *
 * @param int $comment_ID The ID of the comment for which to retrieve the author. Optional.
 * @return string The comment author
 */
function get_comment_author( $comment_ID = 0 ) {
    $comment = get_comment( $comment_ID );
    if ( empty($comment->comment_author) ) {
        if (!empty($comment->user_id)){
            $user=get_userdata($comment->user_id);
            $author=$user->user_login;
        } else {
            $author = __('Anonymous');
        }
    } else {
        $author = $comment->comment_author;
    }
    return apply_filters('get_comment_author', $author);
}

/**
 * Displays the author of the current comment.
 *
 * @since 0.71
 * @uses apply_filters() Calls 'comment_author' on comment author before displaying
 *
 * @param int $comment_ID The ID of the comment for which to print the author. Optional.
 */
function comment_author( $comment_ID = 0 ) {
    $author = apply_filters('comment_author', get_comment_author( $comment_ID ) );
    echo $author;
}

你可以看到你操作了函数get_comment_author给出的返回字符串.
您可以做的是在 functions.php 中添加类似以下内容:

You can see that you van manipulate the return string given by function get_comment_author.
What you can do is add something like the following in your functions.php:

add_filter('get_comment_author', 'my_comment_author', 10, 1);

function my_comment_author( $author = '' ) {
    // Get the comment ID from WP_Query

    $comment = get_comment( $comment_ID );

    if ( empty($comment->comment_author) ) {
        if (!empty($comment->user_id)){
            $user=get_userdata($comment->user_id);
            $author=$user->display_name; // this is the actual line you want to change
        } else {
            $author = __('Anonymous');
        }
    } else {
        $author = $comment->comment_author;
    }

    return $author;
});

希望有帮助!

这篇关于如何显示“自定义昵称"在 Wordpress 评论上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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