在Discuz等论坛经常会看到:”该帖子需要回复后才能看到”,“本链接需要回复才能**”,“如果您要查看本帖隐藏内容请先回复”。

这个功能假如能移植到wordpress是很羡慕的。与Discuz论坛相对而言,Wordpress是很开放的,文章除了密码保护外,文章内容是可以随便查看。

给Wordpress文章增加回复可见,既可防止采集党的肆意转载,保护原创;又可提高互动,增加留言,提高人气等。

1、在 functions.php 中添加下面的代码

  1. //文章内容回复可见
  2. add_shortcode(‘reply’, ‘reply_to_read’);
  3.  
  4. function reply_to_read($atts, $content=null) {
  5. extract(shortcode_atts(array(“notice” => ‘<p>温馨提示:此处内容需要<a href=”#respond” title=”评论本文”>评论本文</a>后才能查看。</p>’), $atts));
  6. $email = null;
  7. $user_ID = (int) wp_get_current_user()->ID;
  8. if ($user_ID > 0) {
  9. $email = get_userdata($user_ID)->user_email;
  10. //对博主直接显示内容
  11. $admin_email = “server-c@qq.com”; //<span style=”color: #0000ff;”>博主Email</span>
  12. if ($email == $admin_email) {
  13. return $content;
  14. }
  15. } else if (isset($_COOKIE[‘comment_author_email_’ . COOKIEHASH])) {
  16. $email = str_replace(‘%40’, ‘@’, $_COOKIE[‘comment_author_email_’ . COOKIEHASH]);
  17. } else {
  18. return $notice;
  19. }
  20. if (empty($email)) {
  21. return $notice;
  22. }
  23. global $wpdb;
  24. $post_id = get_the_ID();
  25. $query = “SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`=’1′ and `comment_author_email`='{$email}’ LIMIT 1”;
  26. if ($wpdb->get_results($query)) {
  27. return do_shortcode($content);
  28. } else {
  29. return $notice;
  30. }
  31. }

其中,博主的EMail设置自己的(上面红色标注),博主可以不用回复就可见。

2、在 style.css 中添加上面代码的显示效果

非必需的,可以不添加,这是CSS样式,可以自己定制样式。。。

  1. .replytoread{
  2. background: #CCCCCC;
  3. color: #000000;
  4. fontfamily: “Microsoft YaHei”,“微软雅黑”,“幼圆”,“宋体”,“楷体”,“楷体_GB2312”;
  5. fontsize: 16px;
  6. fontweight:bold;
  7. fontstyle: normal;
  8. margin:0;
  9. padding: 10px 20px;
  10. lineheight: normal;
  11. }

3、代码使用方法

将需要隐藏的内容用[reple]包裹,编辑文章时,添加:

  1. [reply]你希望评论回复可见的内容[/reply]

  1. [reply notice=”自定义提醒回复内容”]自定义提醒回复内容[/reply]

效果跟你看到的论坛的一样,只是样式可以自己用css定义(步骤2)。


送福利了!关注下方的公众号:“优派编程”,搜索关键词“下载”,即可获得软件app下载资源和python、java等编程学习资料~

更多课程和学习资料请登录“方包博客”———http://fang1688.cn

更多资源请关注公众号或点击下方“阅读原文”,回复关键词获取

1 对 “WordPress文章回复评论才可见内容”的想法;

发表评论

您的电子邮箱地址不会被公开。

+ 49 = 50