请选择 进入手机版 | 继续访问电脑版
查看: 335|回复: 0

WordPress文章内容免插件实现回复可见

[复制链接]

101

主题

5

回帖

998万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9980754
威望
0
金币
9980614
鲜花(0) 鸡蛋(0)
发表于 2020-6-24 10:57:59 | 显示全部楼层 |阅读模式
这个功能类似于Discuz论坛的“回复可见”功能,增加网友间互动的一种方式,以下举两个例子。
实现方式一
将以下代码添加到主题的functions.php文件最后一个?>的前面。

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


PS:将上面代码中的xxxxx@qq.com替换为站点管理员的邮箱地址,以实现管理员直接可见。
使用方法:{reply}测试隐藏内容是否成功{/reply}
{}改成[],所有符号英文字符

实现方式二
核心代码,添加到主题functions.php中

  1. function hide($atts,$content=null,$code=""){
  2. extract(shortcode_atts(array("reply_to_this"=>'true'),$atts));
  3. global $current_user;
  4. get_currentuserinfo();
  5. if($current_user->ID) $email = $current_user->user_email;
  6. if($reply_to_this=='true'){
  7. if($email){
  8. global $wpdb;
  9. global $id;
  10. $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_author_email = '".$email."' and comment_post_id='".$id."'and comment_approved = '1'");
  11. }
  12. if(!$comments) $content = '<div class="hide_notice">抱歉,只有<a href="'.wp_login_url(get_permalink()).'" rel="nofollow">登录</a>并在本文发表评论才能阅读隐藏内容</div>';
  13. }else{
  14. if($email){
  15. global $wpdb;
  16. global $id;
  17. $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_author_email = '".$email."' and comment_approved = '1'");
  18. }
  19. if(!$comments) $content = '<div class="hide_notice">抱歉,只有<a href="'.wp_login_url(get_permalink()).'" rel="nofollow">登录</a>并在本站任一文章发表评论才能阅读隐藏内容</div>';
  20. }
  21. if($comments) $content = '<div class="unhide"><div class="info">以下为隐藏内容:</div>'.$content.'</div>';
  22. return $content;
  23. }
  24. add_shortcode('hide','hide');
复制代码


CSS样式
里面有个图片链接需要自己去扒一图片加上去

  1. .hide_notice{overflow:hidden;padding:8px 8px 8px 24px;border:1px dashed #ff9a9a;background:url(这里是小锁的图片链接,自己扒一下吧...) no-repeat 6px 50%;font-size:12px;color:#ff0000;margin-bottom:15px}
  2. .hide_notice a{color:#ff4b4b}
  3. .unhide{padding:8px;border:1px dashed #ff9a9a;margin-bottom:15px}
  4. .unhide .info{font-size:12px;color:#ff0000}
复制代码


使用方法:
当前文章回复可见:{hide reply_to_this=”true”}测试隐藏内容是否成功{/hide}
任一文章回复可见:{hide reply_to_this=”false”}测试隐藏内容是否成功2{/hide}
{}改成[],所有符号英文字符
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表