[英]Display username to logged in user only?
I have a Wordpress blog set up to display comments as "Anonymous User" by hard coding it into the comments.php file. 我设置了一个Wordpress博客,通过将注释硬编码到comments.php文件中来显示评论为“匿名用户”。 I would like to have it say the user's Username next to their comment and ONLY display that Username to THEM. 我想让他们在评论旁边说出用户的用户名,并且只显示用户名到他们。 In other words, if they're a guest, they'll see "Anonymous User" and if they're a registered/logged in DIFFERENT user, they'll still see "Anonymous User", but if it's THEIR comment it'll say "Your Comment" or their own username. 换句话说,如果他们是客人,他们会看到“匿名用户”,如果他们是注册/登录不同的用户,他们仍然会看到“匿名用户”,但如果它是他们的评论它将会说“你的评论”或他们自己的用户名。 Any clue on a snippet of code? 一段代码的任何线索? Here's what I have so far: 这是我到目前为止所拥有的:
Anonymous User: <div class="post-txt" id="<?php comment_ID() ?>"><?php comment_text() ?></div>
Thanks! 谢谢!
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');
Basically, you will need to get the comment author's ID, get the logged in user's ID and compare the two. 基本上,您需要获取评论作者的ID,获取登录用户的ID并比较两者。 Have a look at getting the current logged in user and getting information about the current comment from the Codex. 查看获取当前登录用户并从Codex 获取有关当前评论的信息 。
I haven't tested this snippet, but it should point you in the right direction: 我没有测试过这个片段,但它应该指向正确的方向:
<?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:';
}
?>
Check if the current visitor is logged in http://codex.wordpress.org/Function_Reference/is_user_logged_in 检查当前访问者是否已登录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.