本文发布于 331 天前,最后更新于331天前,其中的信息可能已经有所发展或是发生改变,如有需要,请联系我(
[email protected])进行更新吖~
前言
隐藏部分文章内容,访客必须先评论才显示。
方法
1.将下面的代码添加到主题的 functions.php 文件:
隐藏的代码
// 通过简码实现部分内容评论后可见
// 编辑文章时,使用下面的简码:
// [reply]评论可见的内容[/reply]
// 或者
// [reply notice="自定义的提示信息"]评论可见的内容[/reply]
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<p class="reply-to-read ">温馨提示:此处内容已隐藏,请<a href="#respond" title="评论本文">评论</a>后刷新页面查看.</p><style>.reply-to-read{border-style:dashed;border-width:3px;border-radius:25px;text-align:center;border-color:blue}</style></p>'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//对博主直接显示内容
$admin_email = "[email protected]"; //博主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');
注意:请修改第13行的邮件为管理员的,这样管理员就可以不用发评论就能看见。
2.编辑文章时,使用下面的简码:
[reply]评论可见的内容[/reply]
或者
[reply notice="自定义的提示信息"]评论可见的内容[/reply]