纯代码实现WordPress文章内容回复可见

这个是类似Discuz论坛的“回复可见”功能,增加网友间互动的一种方式,以下举两个例子。

代码一

将以下代码添加到主题的functions.php文件最后一个?>的前面。

//文章部分内容回复可见
 function reply_to_read($atts, $content=null) {
 extract(shortcode_atts(array("notice" => '
 <span style="color: red;">温馨提示:</span>此处内容需要评论本文后刷新才能查看!'), $atts));
 $email = null;
 $user_ID = (int) wp_get_current_user()->ID;
 if ($user_ID > 0) {
 $email = get_userdata($user_ID)->user_email;
 //对博主直接显示内容
 $admin_email = "xxxxx@qq.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');

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

温馨提示:此处内容需要评论本文后刷新才能查看!

代码二

核心代码,加到主题functions.php

 function hide($atts,$content=null,$code=""){
 extract(shortcode_atts(array("reply_to_this"=>'true'),$atts));
 global $current_user;
 get_currentuserinfo();
 if($current_user->ID) $email = $current_user->user_email;
 if($reply_to_this=='true'){
 if($email){
 global $wpdb;
 global $id;
 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_author_email = '".$email."' and comment_post_id='".$id."'and comment_approved = '1'");
 }
 if(!$comments) $content = '<div class="hide_notice">抱歉,只有<a href="'.wp_login_url(get_permalink()).'" rel="nofollow">登录</a>并在本文发表评论才能阅读隐藏内容</div>';
 }else{
 if($email){
 global $wpdb;
 global $id;
 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_author_email = '".$email."' and comment_approved = '1'");
 }
 if(!$comments) $content = '<div class="hide_notice">抱歉,只有<a href="'.wp_login_url(get_permalink()).'" rel="nofollow">登录</a>并在本站任一文章发表评论才能阅读隐藏内容</div>';
 }
 if($comments) $content = '<div class="unhide"><div class="info">以下为隐藏内容:</div>'.$content.'</div>';
 return $content;
}
add_shortcode('hide','hide');

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

.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}
.hide_notice a{color:#ff4b4b}
.unhide{padding:8px;border:1px dashed #ff9a9a;margin-bottom:15px}
.unhide .info{font-size:12px;color:#ff0000}

使用方法
当前文章回复可见:{hide reply_to_this=”true”}测试隐藏内容是否成功{/hide}
任一文章回复可见:{hide reply_to_this=”false”}测试隐藏内容是否成功2{/hide}
{}改成[],所有符号英文字符

抱歉,只有登录并在本文发表评论才能阅读隐藏内容
抱歉,只有登录并在本站任一文章发表评论才能阅读隐藏内容

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

  1. asdfas asdfas 说道:

    测试看看

  2. sci666 说道:

    代码一失效,评论后刷新也看不见隐藏内容

  3. sci666 说道:

    测试一下评论后可见

  4. 安迪 说道:

    :011: 好棒 看看可否实现