简体   繁体   English

仅当用户发表了自己的评论时才显示 Wordpress 评论

[英]Only show Wordpress comments when the user has posted their own comment

I'm running WordPress site and I need to adjust commenting option.我正在运行 WordPress 站点,我需要调整评论选项。 I want any comments for post be hidden for user until this user has posted his own comment.我希望对用户隐藏任何帖子评论,直到该用户发布他自己的评论。 If user is logged out, everything is hidden except login link.如果用户已注销,则除了登录链接之外的所有内容都将被隐藏。 If user is logged in - comment form is visible but not comments.如果用户已登录 - 评论表单可见但不评论。

What I got so far:到目前为止我得到了什么:

<?php

       if ( post_password_required() ) {
    return;
}
?>

<div id="comments" class="comments-area">

    <?php
    if ( have_comments() ) :
        ?>
        
        <?php

        the_comments_pagination(
            array(
                'prev_text'          => '<span class="screen-reader-text">' . __( 'Previous page', 'spoort' ) . '</span><i class="long-arrow-left" ></i>',
                'next_text'          => '<span class="screen-reader-text">' . __( 'Next page', 'spoort' ) . '</span><i class="long-arrow-right"></i>',
                'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'spoort' ) . ' </span>',
            )
        );
    endif;

    if ( ! comments_open() && get_comments_number() ) :
        echo '<p class="no-comments">' . esc_html_e( 'Comments are closed.', 'spoort' ) . '</p>';
    endif;
    comment_form();
        ?>
    
    <?php if ( is_user_logged_in() ) { ?>
            <ol class="comment-list">
        <?php
        wp_list_comments(
            array(
                'style'       => 'ol',
                'avatar_size' => 48,
                'short_ping'  => true,
            )
        );
        ?>
        </ol>
</div>
        <?php } else { ?>
<?php } ?>

The code you supplied looks like it's trying to do things in the wrong order and/or in the wrong places.您提供的代码看起来像是试图以错误的顺序和/或在错误的地方做事。 For example, if ( have_comments() ): will display the pager, but you have that outside of the logic for whether comments should be displayed or not, so it's very possible to have someone see a pager but no comments, which is not good.例如, if ( have_comments() ):将显示寻呼机,但是您在是否应显示评论的逻辑之外拥有它,因此很可能有人看到寻呼机但没有评论,这不好.

The code you gave is a little unclear, but I believe you want the following:您提供的代码有点不清楚,但我相信您想要以下内容:

<?php

if ( post_password_required() ) {
    return;
}
?>

<?php
$display_comments = false;

if(comments_open()) {
    if(is_user_logged_in()) {
        ?>
        <div id="comments" class="comments-area">

        <?php
        //Go through all the comments on the post.
        foreach(get_comments() as $current_comment) {
            if($current_comment->comment_author == get_current_user_id()) {
                $display_comments = true;
            }
        }


        if(have_comments() && $display_comments) {
            the_comments_pagination(
                array(
                    'prev_text'          => '<span class="screen-reader-text">' . __( 'Previous page', 'spoort' ) . '</span><i class="long-arrow-left" ></i>',
                    'next_text'          => '<span class="screen-reader-text">' . __( 'Next page', 'spoort' ) . '</span><i class="long-arrow-right"></i>',
                    'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'spoort' ) . ' </span>',
                )
            );
            comment_form();

            <ol class="comment-list">
            <?php
            wp_list_comments(
                array(
                    'style'       => 'ol',
                    'avatar_size' => 48,
                    'short_ping'  => true,
                )
            );
            ?>
            </ol>
            <?php
        } else {
            comment_form();
        }

        ?>
        </div>
        <?php
    }


}








if(!comments_open() || !get_comments_number()) {
    echo '<p class="no-comments">' . esc_html_e( 'Comments are closed.', 'spoort' ) . '</p>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM