有条件:如果当前用户没有对当前帖子发表评论(不包括帖子作者)

时间:2018-02-26 作者:Pete

我正在试图找到一种方法,在single.php 哪里//do something 如果当前用户尚未对当前帖子发表评论,则会发生此情况。

我对编码毫无希望,但我想我需要一些这个。。。

is_user_logged_in()

$user_id = get_current_user_id()
评论部分让我困惑。。。这有用吗?https://wordpress.stackexchange.com/questions/137799/how-to-limit-users-to-one-comment-per-post

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

[编辑]以下是修改后的代码:

$post_id = get_the_ID();          // ID of the current post.
$user_id = get_current_user_id(); // ID of the current user.
$post_author_id = get_post_field( \'post_author\', $post_id );
$is_post_author = ( $user_id && $post_author_id == $user_id );
$can_comment = $is_post_author;

/*
 * If $can_comment is not yet TRUE, then it\'s because either:
 *   a) The user is not the author of the post; or
 *   b) The user is not registered on the site; or
 *   c) The user is logged-out.
 */
if ( $user_id && ! $can_comment ) {
    $comment_count = get_comments( array(
        \'post_id\' => $post_id,
        \'user_id\' => $user_id,
        \'count\'   => true,
    ) );

    $can_comment = empty( $comment_count );
}

if ( ! $is_post_author ) {
    if ( $can_comment ) {
        echo \'Post your comment.\';
    } else {
        echo \'Sorry, you\\\'ve commented. Or you can\\\'t comment..\';
    }
}

结束