1 比较普通的CSS的代码

在主题的function.php中加入以下代码:

function reply_to_read($atts, $content=null) {
        extract(shortcode_atts(array("notice" => '<p class="reply-to-read" style="border-width: 1px 1px 1px 1px;border-color: #F2F2F2;line-height: 150%;"><blockquote><font color="#ff0000"><b>温馨提示</b></font>: 隐藏内容需要<a href="#respond" title="点击进行评论"> 回复评论 </a>后才能查看, 评论后请 <strong><a href="javascript:location.reload()" title="点击刷新"> 刷新 !</a></strong>.</blockquote></p>'), $atts));
        $email = null;
        $user_ID = (int) wp_get_current_user()->ID;
        if ($user_ID > 0) {
            $email = get_userdata($user_ID)->user_email;
            //对博主直接显示内容
            $admin_email = "xxxxxxx@xx.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');

2 增加了红点边框的CSS的代码

在主题的function.php中加入以下代码:

function reply_to_read($atts, $content=null) {
        extract(shortcode_atts(array("notice" => '<p class="reply-to-read" style="text-align:center; border:2px solid #f00; border-style:dotted; border-radius:4px; padding:5px; margin:10px;"><strong style="color: red;">温馨提示:</strong>为了避免资源链接被和谐,此处内容需要您<strong><a href="#respond" title="点击进行评论"> 回复评论 </a></strong>后才能查看, 评论后请 <strong><a href="javascript:location.reload()" title="点击刷新"> 刷新!</a></strong></p>'), $atts));
        $email = null;
        $user_ID = (int) wp_get_current_user()->ID;
        if ($user_ID > 0) {
            $email = get_userdata($user_ID)->user_email;
            //对博主直接显示内容
            $admin_email = "xxxxxxx@xx.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');

3 修改CSS样式

大家可以根据自己的需要修改上述代码extract语句中css自己定制化的css样式。

样式示例:

在本站注册+登录账号并在此文章下评论即可查看隐藏内容! 去注册 去登录 去评论

(1) 本网站资源来自于网络收集,仅供学习交流使用,请下载者在24小时内删除下载的资源,如下载者将此资源用于其他非法用途,本站不承担任何法律责任;如有侵权请联系站长删除!
(2) 建议购买正版资源,支持原作者;
(3) 如资源链接失效,可加入QQ群526867211或者发送邮件到stubbornhuang@qq.com进行反馈;
(4) 由于本站服务器性能问题,为了减少服务器负载,防止宕机,自2021年4月28日起限制每一个用户24小时内可评论次数为3次;
(5) 如果觉得分享的资源对您有帮助,也可扫描下方二维码,对站长进行赞助,感谢各位金主爸爸!

4 使用方法

WordPress – 为文章增加评论才可查看相关隐藏内容的代码-StubbornHuang Blog

5 注意的点

如果站点在后台设置了评论审核,那么游客评论之后需要站点管理员审核评论通过之后刷新页面才会看到隐藏内容,所以如果要使用该功能建议关闭评论审核功能。