阻止参与者显示评论列表

时间:2014-11-03 作者:user186585

我如何才能更改WP,使投稿者不会显示对其他帖子的评论,从而只看到对其帖子的评论?

1 个回复
SO网友:Subharanjan

将此代码粘贴到functions.php 当前活动主题的文件。你也可以把它变成一个插件文件。它使用“the_comments“筛选以从后端表中删除不需要的注释。

/**
 * Filter out comments of other posts than current logged-in user.
 *
 * @param $all_comments all the comments to be displayed in List_Table
 *
 * @return mixed filtered out comments.
 */
function filter_comments_by_contributor( $all_comments ) {
    // get the current logged in user
    $current_user = wp_get_current_user();

    if ( 0 == $current_user->ID ) {
        // Not logged in.
        return $all_comments;
    } else {
        // Logged in.

        // check if the logged-in user is a contributor
        if ( in_array( \'contributor\', (array) $current_user->roles ) ) {

            // check if the user is on wp-admin backend,
            $screen = get_current_screen();
            if ( ! empty( $screen ) && \'edit-comments\' == $screen->id ) {

                // get all posts by that contributor
                $args              = array(
                    \'author\'         => $current_user->ID,
                    \'posts_per_page\' => - 1,
                    \'fields\'         => \'ids\'
                );
                $contributor_posts = get_posts( $args );

                // unset the comments given on posts other than his/her.
                foreach ( $all_comments as $key => $value ) {
                    if ( ! in_array( $value->comment_post_ID, $contributor_posts ) ) {
                        unset( $all_comments[ $key ] );
                    }
                }
            }

            return $all_comments;
        } else {
            return $all_comments;
        }
    }
}
add_filter( \'the_comments\', \'filter_comment_by_contributor\' );

结束

相关推荐

comments in Admin

我对管理员中帖子的评论部分实际上做了什么感到困惑。我在帖子的屏幕选项中打开了评论。当我显示帖子时,我会看到一个评论框。如果我在评论框中输入了什么,它会发生什么?当我在管理中单击“显示评论”时,我从未看到它。但在某些主题上,它会出现在帖子中。