仅在用户发布自己的评论时显示Wordpress评论

问题描述

我正在运行WordPress网站,我需要调整评论选项。我希望对该用户隐藏任何帖子评论,直到该用户发表自己的评论为止。如果用户已注销,则除了登录链接外,其他所有内容均被隐藏。如果用户登录-可见评论表单,但没有评论

到目前为止,我得到的是:

<?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 } ?>

解决方法

您提供的代码看起来像是试图以错误的顺序和/或在错误的位置执行操作。例如,if ( have_comments() ) :将显示传呼机,但是您在逻辑上不考虑是否应显示评论,因此很可能让某人看到传呼机但没有评论,这是不好的。 / p>

您提供的代码不清楚,但我相信您需要以下内容:

<?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>';
}