会有很多人在帖子上发表评论。因此,当评论员访问《华盛顿邮报》时,他应该“只能”看到自己的评论。帖子作者只能在自己的帖子上看到所有评论。
如何在WordPress中执行此操作?
会有很多人在帖子上发表评论。因此,当评论员访问《华盛顿邮报》时,他应该“只能”看到自己的评论。帖子作者只能在自己的帖子上看到所有评论。
如何在WordPress中执行此操作?
通过使用“pre\\u get\\u comments”操作,可在中找到wp-includes/comment.php
:
function restrict_visible_comments( $comments_query ) {
if ( !is_singular() )
return;
if ( current_user_can( \'moderate_comments\' ) )
return; // moderators can see all comments
if ( get_current_user_id() == get_queried_object()->post_author )
return; // the author of the post can see all comments
$comments_query->query_vars[\'user_id\'] = get_current_user_id();
}
if ( !is_admin() )
add_action( \'pre_get_comments\', \'restrict_visible_comments\' );
请注意,以上内容不会显示未注册用户留下的评论。Update: 无论如何,这行不通,因为get\\u comments()在comments\\u template()中使用不一致:
http://core.trac.wordpress.org/browser/tags/3.1.3/wp-includes/comment-template.php#L882
使用comments\\u数组过滤器,删除不必要的注释。
一些松散的代码让你开始
<?php
add_filter(\'comments_array\',\'display_only_user_comments\')
function display_only_user_comments($comments){
$comemnts=NULL;
/*modify this part to get only user comments*/
if ( $user_ID) {
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = \'1\' OR ( user_id = %d AND comment_approved = \'0\' ) ) ORDER BY comment_date_gmt", $post->ID, $user_ID));
} else if ( empty($comment_author) ) {
$comments = get_comments( array(\'post_id\' => $post->ID, \'status\' => \'approve\', \'order\' => \'ASC\') );
} else {
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = \'1\' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = \'0\' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email));
}
return $comments;
}
?>
@萨蒂什。。。谢谢你,伙计!我不知道你的解决方案是否对提问者有效,但它对我有效!我花了两天时间尝试了“comments\\u子句”和“pre\\u get\\u comments”这两个词,但我已经放弃了!
我的解决方案是让作者只看到自己的评论
add_filter(\'pre_get_comments\',\'display_only_user_comments\');
function display_only_user_comments($query){
global $pagenow;
if(\'edit-comments.php\' != $pagenow || $query->is_admin)
return $query;
if( !current_user_can( \'manage_options\' ) ) {
global $user_ID;
$query->query_vars[\'post_author\'] = $user_ID;
}
return $query;
}
Useful Comments Plugin..如果您对编码不太了解,则表示插件是最简单的方法。:)我希望你得到答案……:)
我已经安装了NoSpamMX插件。我很长时间没有收到垃圾评论了。但现在我收到了很多垃圾评论,尤其是在一篇帖子中。为什么只有那个帖子才会有这么多垃圾邮件?