首页 > Wordpress > 纯代码实现wordpress文章隐藏内容评论可见
2014
07-23

纯代码实现wordpress文章隐藏内容评论可见

在社区论坛里,楼主发表帖子的时候一般会把重要内容隐藏起来,用户回复帖子后才能看见,有效地避免看帖不回,提高论坛的活跃度。把该功能引进wordpress网站,也能有效地提高网站的评论数量(当然垃圾评论也会相继增加),提高wordpress站点的活跃度。前面介绍过使用Easy2Hide插件实现该功能效果,本篇内容是介绍通过纯代码实现wordpress文章内容评论可见。
在当前主题的functions.php文件添加以下代码

  1. <?php  
  2.     function reply_to_read($atts, $content=null) {  
  3.         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));  
  4.         $email = null;  
  5.         $user_ID = (int) wp_get_current_user()->ID;  
  6.         if ($user_ID > 0) {  
  7.             $email = get_userdata($user_ID)->user_email;  
  8.             //对博主直接显示内容  
  9.             $admin_email = “xxx@mf158.com”//把左面的邮箱换成博主Email  
  10.             if ($email == $admin_email) {  
  11.                 return $content;  
  12.             }  
  13.         } else if (isset($_COOKIE[‘comment_author_email_’ . COOKIEHASH])) {  
  14.             $email = str_replace(‘%40‘, ‘@’, $_COOKIE[‘comment_author_email_’ . COOKIEHASH]);  
  15.         } else {  
  16.             return $notice;  
  17.         }  
  18.         if (empty($email)) {  
  19.             return $notice;  
  20.         }  
  21.         global $wpdb;  
  22.         $post_id = get_the_ID();  
  23.         $query = “SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`=’1′ and `comment_author_email`='{$email}’ LIMIT 1”;  
  24.         if ($wpdb->get_results($query)) {  
  25.             return do_shortcode($content);  
  26.         } else {  
  27.             return $notice;  
  28.         }  
  29.     }  
  30.     add_shortcode(‘reply’, ‘reply_to_read’);  
  31. ?>  

注:把代码中的“xxx@mf158.com”换成博主邮箱地址

编辑文章的时候,使用[reply][/reply]把要评论可见的内容包围起来,如:

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

文章会显示:

  1. 温馨提示: 隐藏内容需要 回复评论 后才能查看, 评论后请 刷新 !.  

也可以使用格式:

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

自定义温馨提示的内容。

提醒:如果开通了评论审核,那么内容需要评论审核通过后才可以看见!

最后编辑:
作者:漱石
这个作者貌似有点懒,什么都没有留下。
捐 赠如果您觉得这篇文章有用处,请支持作者!鼓励作者写出更好更多的文章!

纯代码实现wordpress文章隐藏内容评论可见》有 1 条评论

留下一个回复