仅向帖子作者显示WordPress评论

时间:2016-05-24 16:20:54

标签: php wordpress wordpress-theming

在新的WordPress模板中,我想自定义 single.php ,以便仅向帖子作者显示评论列表。

其他用户(访客和其他帖子的作者)应该看不到任何评论或空白。

总之,帖子作者(记录时)可以看到帖子底部的评论列表,但其他作者写的帖子底部没有任何内容。

我应该将哪些条件代码应用于以下代码?

<?php 
global $wpdb,$current_user;
$limit = 10; //this is limit per day per user
$comment_count = $wpdb->get_var( $wpdb->prepare("
SELECT count(*)
FROM cxp_comments
WHERE comment_author = '%s'
AND comment_date >= DATE_SUB(NOW(),INTERVAL 1 DAY);"
,$current_user->user_login) );

if($comment_count < $limit): ?>

<h2 class="comments-title"> Want to get and review this product?</h2>
<form action="http://localhost/reviews/wp-comments-post.php" method="post"     id="commentform" class="comment-form" novalidate="">
<p class="comment-amaz" style="display:none;"><input id="user-rank"     name="user-rank" aria-required="false" class="" type="text" value="<?php echo     do_shortcode('[mycred_my_ranking]'); ?>"></p>
<p class="comment-amazi" style="display:none;"><input id="user-points"     name="user-points" aria-required="false" class="" type="text" value="<?php echo     do_shortcode( '[mycred_my_balance wrapper="0" title_el="" balance_el=""]' ); ?>">    </p>
<p class="comment-form-comment">
<textarea id="comment" name="comment" placeholder="Insert your Amazon Public     Profile Link" aria-required="true"></textarea></p>
<p class="form-submit"><input name="submit" id="submit" class="submit"     value="Review this product!" type="submit"> <input name="comment_post_ID" value="    <?php global $post;
echo $post->ID; ?>" id="comment_post_ID" type="hidden">
<input name="comment_parent" id="comment_parent" value="0" type="hidden">
</p> </form>
<?php endif; ?>


<?php if($comment_count > $limit): ?>
<p>Exceeded comments limit for today. Please, come back tomorrow!</p>

<?php endif; ?>

这仅适用于前端。我不关心管理员小组。

1 个答案:

答案 0 :(得分:1)

检查用户是否使用is_user_logged_in()登录:

Param([Parameter(Mandatory=$True,Position=1)]
       [string]$user,
  [Parameter(Mandatory=$True,Position=2)]
       [string]$pass,
  [Parameter(Mandatory=$True,Position=3)]
       [string]$dir,
  [Parameter(Mandatory=$True,Position=4)]
       [string]$fileName,
  [Parameter(Mandatory=$True,Position=5)]
       [string]$url 
 )

$filePath = ("$dir" + "$fileName")

$webClient = New-Object System.Net.WebClient;
    $webClient.Credentials = New-Object System.Net.NetworkCredential("$user", "$pass");
    ("*** Uploading {0} file to {1} ***" -f ($filePath, $url) ) | write-host -ForegroundColor Blue -BackgroundColor White
    $webClient.UploadFile($url, "PUT", $filePath);

然后使用wp_get_current_user()

获取用户登录信息
var userPostCount;

    userPostsModel.findOne({'profileID':req.session.id}, function(err, usersPostCountDB) {
        if(usersPostCountDB){
            userPostCount = req.body.postsCount + 1;
        } else {
            console.log('There was an error getting the postsCount');
       }
    });

然后使用get_the_author_meta()获取作者登录信息:

if(is_user_logged_in())

最后,比较一下:

$curUser = wp_get_current_user();
$curLogin = $curUser->user_login;

完整代码:

$autLogin = get_the_author_meta('user_login');