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

时间:2010-06-11 14:00:29

标签: php wordpress comments

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

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

谢谢!

3 个答案:

答案 0 :(得分:2)

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');

答案 1 :(得分:2)

基本上,您需要获取评论作者的ID,获取登录用户的ID并比较两者。请查看食典委的getting the current logged in usergetting information about the current comment

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

<?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:';
    }
?>

答案 2 :(得分:1)

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

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