2016-11-11 115 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