繁体   English   中英

仅显示登录用户的用户名?

[英]Display username to logged in user only?

我设置了一个Wordpress博客,通过将注释硬编码到comments.php文件中来显示评论为“匿名用户”。 我想让他们在评论旁边说出用户的用户名,并且只显示用户名到他们。 换句话说,如果他们是客人,他们会看到“匿名用户”,如果他们是注册/登录不同的用户,他们仍然会看到“匿名用户”,但如果它是他们的评论它将会说“你的评论”或他们自己的用户名。 一段代码的任何线索? 这是我到目前为止所拥有的:

Anonymous User: <div class="post-txt" id="<?php comment_ID() ?>"><?php comment_text() ?></div>

谢谢!

function my_custom_comment_author_filter($author){
  global $current_user;
  wp_get_current_user();
  if(!is_category(3)){
    return $author;
  }
  if(0 == $current_user->ID || ($current_user->display_name !== $author && $current_user->user_login !== $author)){
    return 'Anonymous User';
  }
  return $author;
}

add_filter('get_comment_author', 'my_custom_comment_author_filter');

基本上,您需要获取评论作者的ID,获取登录用户的ID并比较两者。 查看获取当前登录用户并从Codex 获取有关当前评论的信息

我没有测试过这个片段,但它应该指向正确的方向:

<?php global $user_id, $user_login; 
    get_currentuserinfo();  // This will populate $user_id with the logged in user's ID or '' if not logged in
    $the_comment = get_comment(comment_ID());  // Get a comment Object...
    $author_id = $the_comment->user_id; // and extract the commenter's ID

    if($user_id !== '' && $author_id == $user_id){
        echo 'Your comment [ ' . $user_login . ' ]:';
    }
    else{
        echo 'Anonymous User:';
    }
?>

检查当前访问者是否已登录http://codex.wordpress.org/Function_Reference/is_user_logged_in

<?php if ( is_user_logged_in() ) { 
    ....
} else {
    ....
} ?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM