有时候我们在写文章的时候,往往不希望读者能够支持一下作者的良苦用心,下面的代码可以让读者在评论回复之后才能看见所隐藏的内容
添加代码
将下面的代码添加到主题的 functions.php 文件:
//部分内容评论可见
function reply_to_read($atts, $content = null) {
extract(shortcode_atts(array(
"notice" => '<blockquote><center><p class="reply-to-read" style="color: blue;">注意:本段内容须成功“<a href="' . get_permalink() . '#respond" title="回复本文">回复本文</a>”后“<a href="javascript:window.location.reload();" title="刷新本页">刷新本页</a>”方可查看!</p></center></blockquote>'
) , $atts));
$email = null;
$user_ID = (int)wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//对博主直接显示内容
$admin_email = get_bloginfo('admin_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');
调用方法
编辑文章时,使用下面的两种简码调用:
删除第一个reply的空格,把斜杠替换为/
注意:此方法对QQ快速登录的用户是无效的。
//简码1 任选一种使用 [ reply]评论可见的内容[斜杠reply] //简码2 任选一种使用 [ reply notice="自定义的提示信息"]评论可见的内容[斜杠reply]
效果演示
此处内容需要 登录 并发表评论才可查看
阅读全文
轻语博客







1狠狠
功能测试
看看效果咋样