WordPress 隐藏部分文章内容 评论后才可见

张子凡 WordPress优化 2014-12-20 06:42:33 阅读(...) 评论(0)

刚刚在博客看到有留言咨询 WordPress 发表一篇文章,读者评论后才能看!就这么一个功能,以前也没有怎么注意这个功能,不过感觉这个功能也挺实用的,泪雪主题之前其实也有这个功能的,不过后来优化和取舍之间就删除了这个功能,既然有朋友需要,那么我就整理一篇完整的文章出来供大家参考吧!大家可以根据下面方法自行添加代码即可。

1.将下面的代码添加到主题的 functions.php 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//实现部分内容评论后可见
function reply_to_read($atts, $content=null) {   
        extract(shortcode_atts(array("notice" => '<p class="reply-to-read">温馨提示: 此处内容需要<a href="#respond" title="评论本文">评论本文</a>后才能查看.</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 = "leixue@leiue.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');

注:请自行修改第 9 行的邮件为管理员的。如果你的网站使用了 ajax 免刷新提交评论,你可能需要修改第 3 行的提示文字,提示访客评论后刷新页面来查看隐藏内容。

2.编辑文章时,使用下面的简码:

1
[reply]评论可见的内容[/reply]

1
[reply notice="自定义的提示信息"]评论可见的内容[/reply]

更多关于WordPress优化及疑问可以添加QQ群:255308000

除非注明,否则均为泪雪博客原创文章,禁止任何形式转载

本文链接:https://zhangzifan.com/wordpress-reply-to-view-content.html

留言评论

登录 后留言