WordPress实现评论后可显示内容的方法

简介

内容来源于网络搜集

先看下使用样式,结构比较简单,我一般都是套用其他的样式一起使用,这样才会突出显示

由于WP主题不支持在文章中的显示内容不可见,而且,也没有找到类似的插件,所以在网上找了一些代码,勉强使用以下

function reply_to_read($atts, $content=null) {
    extract(shortcode_atts(array("notice" => '温馨提示: 此处内容需要评论本文后才能查看.'), $atts));
    $email = null;
    $user_ID = (int) wp_get_current_user()->ID;
    if ($user_ID > 0) {
      $email = get_userdata($user_ID)->user_email;
      //对博主直接显示内容
      $admin_email = "xxx@aaa.com"; //博主Email
      if ($email == $admin_email) {
        return $content;
      }
    } else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) {
      $email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]);
    } else {
      return $notice;
    }
    if (empty($email)) {
      return $notice;
    }
    global $wpdb;
    $post_id = get_the_ID();
    $query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1";
    if ($wpdb->get_results($query)) {
      return do_shortcode($content);
    } else {
      return $notice;
    }
}
add_shortcode('reply', 'reply_to_read');

不过使用时需要注意的是:

要修改第8行的邮件为管理员的。如果你的网站使用了ajax免刷新提交评论,应该还需要修改第2行的提示文字,提示访客评论后刷新页面来查看隐藏内容。

使用方法

将以上代码复制到主题编辑文件functions.php中使用,使用前记得先备份源文件

另外在编辑文件时,直接插入以下代码即可

【reply】评论可见的内容【/reply】或 
【reply notice="自定义的提示信息"】评论可见的内容【/reply】
注意其中的符号是英文下的[]
Comments | NOTHING

    空空如也!

消息盒子
# 您需要首次评论以获取消息 #
# 您需要首次评论以获取消息 #

只显示最新10条未读和已读信息