1、将下面的代码添加到主题的 functions.php 文件:
- 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 = "fujieace@126.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 (emptyempty($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行的提示文字,提示访客评论后刷新页面来查看隐藏内容。
2、编辑文章时,切换到“文本”状态下:使用下面的代码:
- 【reply】评论可见的内容【/reply】
或者
- 【reply notice="自定义的提示信息"】评论可见的内容【/reply】
3、第2步的【】 全部 替换成 []
我说一下为什么要这样替换,其实我也想这样直接把源码直接展示出来给大家,但是如果我直接这样走,html它会直接给我解析了,这就没办法了;只好出此下策;
注意:此代码在以前是只要不评论都不可见隐藏内容,为了用户体验更好,后来改进了功能,添加了“对管理员不隐藏”的判断。
不管你是用wordpress评论可见插件:easy2hide;还是用本页的代码,你一定要知道它们各自的优点和缺点,如果我们用此页的代码开启功能,那么:每一个隐藏的内容是不是需要手工加代码?如果有一天就算你删除了 functions.php里面的文件,但是你的网站内容页依旧还是有这些代码的。你总不能一个一个都去手工删除吧!
反之,用插件却不一样了,插件可以随时启用,也可关闭。并不影响!唯一的是:插件可能会影响一些服务器的性能,如果你是云服务器,这些都不是事;一般来说,只要wordpress插件不超过15个,我觉得都还是蛮正常的;
2017年03月24日 21:36:44 沙发
为什么不直接用插件?插件方便的多。