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

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

匿名用户:< 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');
上一篇:WordPress 不错的插件


下一篇:我该如何评论部分Python函数?