改进WordPress侧边栏最新评论功能:不显示自己(作者)的评论+直接显示评论内容

Type My Life刚开张不久,来捧场的好友都留了言,我当然得回复一下。不过,一阵回复后才发觉,WordPress侧边栏自带的最新评论功能太简陋了,不符合实际使用要求。

具体问题如下:

1)所有留言的具体内容都不会显示出来。默认显示格式为:“读者ID”+“在”+“具体文章名”+“上的评论”。

2)作者(站长)自己的留言也会显示出来。这样一来,当作者连续回复时,最新评论就都是作者自己的留言了,这个模块就丧失了其应有的功能。

针对以上两个问题,处理方法也不少,有使用插件的(如WP-RecentComments),也有修改代码的。按照我的习惯,不太复杂的改动尽量不使用插件,以免拖累网站运行速度。因此,本文将介绍如何通过修改代码来解决以上问题。

1)让最新留言的具体内容直接显示出来

进入你的网站根目录,在WordPress源程序文件夹中的/wp-includes/路径下找到default-widgets.php,打开编辑之。

搜索到以下代码片段:

foreach ( (array) $comments as $comment) {
$output .=  ‘<li>’ . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x(‘%1$s on %2$s’, ‘widgets’), get_comment_author_link(), ‘<a href=”‘ . esc_url( get_comment_link($comment->comment_ID) ) . ‘”>’ . get_the_title($comment->comment_post_ID) . ‘</a>’) . ‘</li>’;
}

修改步骤一:

把(_x(‘%1$s on %2$s’, ‘widgets’)里面的这个单词“on”改成冒号“:”。

修改步骤二:

把get_the_title($comment->comment_post_ID)改为mb_strimwidth(strip_tags($comment->comment_content),0,50, 。。。》)。 这里的数字“50”是用来限制评论显示的字数,可以自行修改,至于后边那个小尾巴”。。。》”则是用来在实际评论字数少于允许显示的字数时补充空白处的,也可以依自己喜欢的格式修改之。

以上修改完成后,最新评论的格式就变为:“读者ID”+”:”+“实际评论内容”。

2)让最新评论不显示作者自己的评论

修改对象依然是上面提到的default-widgets.php文件。

搜索到以下代码片段:

$comments = get_comments( apply_filters( ‘widget_comments_args’, array( ‘number’ => $number, ‘status’ => ‘approve’, ‘post_status’ => ‘publish’ ) ) );

修改为以下格式:

$comments = get_comments( apply_filters( ‘widget_comments_args’, array( ‘number’ => $number, ‘status’ => ‘approve’, ‘post_status’ => ‘publish’, ‘type’ => ‘comment’, ‘user_id’ => 0 ) ) );

解释一下:’user_id’ => 0效果为不显示站长自己的回复,’type’ => ‘comment’效果为只显示评论类留言,即,不显示pingback和trackback类留言。

【参考文章】

1. 不用插件利用wordpress自带的代码显示最新评论

2. 让wordpress最近评论中不显示自己的评论

Update: 2013-08-11 本文内容经测试对WordPress 3.6版本仍有效,其他版本不保证效果。

“改进WordPress侧边栏最新评论功能:不显示自己(作者)的评论+直接显示评论内容”的38个回复

  1. 你好,我找到的代码是这样的
    $comments = get_comments( array( ‘number’ => $number, ‘status’ => ‘approve’, ‘post_status’ => ‘publish’ ) );
    我修改成了这样
    $comments = get_comments( array( ‘number’ => $number, ‘status’ => ‘approve’, ‘user_id’ => 0 ) );
    没见有变动?请问是哪里出错了?

    1. 我查看了一下,可能是版本更新后有所变动,应该改成
      $comments = get_comments( array( ‘number’ => $number, ‘status’ => ‘approve’, ‘post_status’ => ‘publish’, ‘type’ => ‘comment’, ‘user_id’ => 0 ) );

  2. 谢谢教程!试了之后基本都成功了,就是改成这样后后面的小尾巴没显示出来

    $output .= ”;
    if ( $comments ) {
    foreach ( (array) $comments as $comment) {
    $output .= ” . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x(‘%1$s : %2$s’, ‘widgets’), get_comment_author_link(), ‘comment_ID) ) . ‘”>’ . mb_strimwidth(strip_tags($comment->comment_content),0,50, 。。。》) . ‘‘) . ”;
    }

    这是为什么呢……

  3. 仅仅会复制粘贴……的表示为什么之前评论粘贴的内容和我原本的不太一样……

    $output .= ”;
    if ( $comments ) {
    foreach ( (array) $comments as $comment) {
    $output .= ” . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x(‘%1$s : %2$s’, ‘widgets’), get_comment_author_link(), ‘comment_ID) ) . ‘”>’ . mb_strimwidth(strip_tags($comment->comment_content),0,50, 。。。》) . ‘‘) . ”;
    }
    }
    $output .= ”;
    $output .= $after_widget;

    最终效果那个粘贴了一下博客就整个消失了oo

    1. 呃,可能是代码直接贴上来会被转义,就好像不能直接在日志中贴代码一样(最好是在html界面下编辑)。

  4. 感谢博主,我成功了

    贵站使用版权声明:署名-非商业性使用-禁止演绎 | Creative Commons BY-NC-ND 3.0
    我不能引用或转载文章吗

    1. 后来我用了第三方评论系统(多说),不用系统自带的侧边栏评论了,也能实现类似效果,无需修改代码。

回复 Zhang 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注