2016-11-11 116 views
0

我得到如何在WordPress的帖子中顯示評論的作者(未註冊)未批准的評論?

$comments = get_comments(array(
    'post_id'=>get_the_ID(), 
    'order'=>'ASC', 
)); 

我展示與環中檢查$comment->comment_approved評論意見。 但我想向發送評論的人顯示未經批准的評論。

我的解決方案是檢查$_SERVER['HTTP_USER_AGENT'] == $comment->comment_agent在所有評論(批准與否)的循環,但我不確定,這是否足夠?!或者必須檢查其他值?

+0

我不明白!你爲什麼要檢查'comment_approved',但你還想要未經批准的評論? –

+0

我想爲所有訪問者顯示已批准的評論,除非有未批准的評論顯示未經批准的評論; [像這樣](http://s8.picofile.com/file/8274404368/sample.png)。我會爲任何線索或提及感到高興。 – Yahya

回答

0

這是最好的解決方案添加這一點,我發現。

// geting comments of post 
$comments = get_comments(array(
    'post_id'=>get_the_ID(), 
    'order'=>'ASC', 
)); 

foreach($comments as $comment){ 
    // if unapproved comment is not commented by this user continue... 
    if(!$comment->comment_approved && $_SERVER['HTTP_USER_AGENT'] != $comment->comment_agent){ 
     continue; 
    } 
    // if unapproved comment is commented by this user show the message. 
    if(!$comment->comment_approved && $_SERVER['HTTP_USER_AGENT'] == $comment->comment_agent){ 
     echo "Your comment is awaiting moderation"; 
    } 
    // show the comment 
    echo $comment->comment_content . '<br>';  
} 
0

試試這個:

添加下面的代碼,你需要的地方註釋

$comments = get_comments(array(
    'post_id'=> get_the_ID(), 
    'order'=>'ASC', 
)); 

if (is_user_has_unapproved ($comments, get_current_user_id())) { 
    echo "Your comment is awaiting moderation"; 
} else { 
    foreach ($comments as $comment) { 
     echo $comment->comment_content."<br>"; 
    } 
} 

functions.php

function is_user_has_unapproved ($comments, $user_id) { 
    $i = 0; 
    foreach ($comments as $comment) { 
     if ($comment->user_id == $user_id) { 
      if ($comment->comment_approved == "0") { 
       $i++; 
      } 
     } 
    } 

    if ($i != 0) { 
     return true; 
    } else { 
     return false; 
    } 
} 

好運

+0

但在此解決方案中;如果帖子有任何未經批准的評論,則沒有人可以看到任何評論(甚至已批准的評論)。因爲'get_current_user_id()'返回所有普通訪問者(未註冊)的'0'。看這是我的解決方案,是否可以嗎? '的foreach($意見爲$評論){ \t \t如果{ \t \t \t繼續($ comment-> comment_approved && $ _ SERVER [ 'HTTP_USER_AGENT'] = $ comment-> comment_agent!); \t \t} \t \t如果($ comment-> comment_approved && $ _ SERVER [ 'HTTP_USER_AGENT'] == $ comment-> comment_agent!){ \t \t \t回聲 '您的評論正在等待審覈'; \t \t} \t \t echo $ comment-> comment_content; \t \t}' – Yahya

+0

首先爲什麼'__SERVER ['HTTP_USER_AGENT']'??? –

+0

因爲wordpress會在每條評論的comment_agent列的評論表中保存user_agent。我們可以將它與當前訪問者user_agent進行比較。我的問題是:我們不需要更多的檢查,如IP或其他?或者我的解決方案完全可以嗎?!畢竟感謝你的耐心。 – Yahya