簡體   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