之前曾经分享过一篇关于Easy2Hide插件的文章,文章介绍了如何使用该插件实现隐藏内容的方法与技巧。但是很多人反馈不喜欢使用插件去实现这些功能,那这篇文章就是介绍wordpress不用插件如何实现文章隐藏内容评论可见。
在当前主题的functions.php文件添加以下代码:
< ?php
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '
< 建站网blockquote>温馨提示: 隐藏内容需要< 建站网a href="#respond" title="点击进行评论"> 回复评论 后才能查看, 评论后请 < 建站网a href="javascript:location.reload()" title="点击刷新"> 刷新 !< 建站网/a>.'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//对博主直接显示内容
$admin_email = "xxx@zhushuiwen.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');
?>
注:把代码中的“xxx@zhushuiwen.com”换成博主邮箱地址,将出现的建网站字眼删掉!
编辑文章的时候,使用 把要评论可见的内容包围起来,如:
文章会显示:
温馨提示: 隐藏内容需要 回复评论 后才能查看, 评论后请 刷新 !.
自定义的提示信息
自定义温馨提示的内容。
提醒:如果开通了评论审核,那么内容需要评论审核通过后才可以看见!
438770 169292Most reliable human being messages, nicely toasts. are already provided gradually during the entire wedding celebration and therefore are anticipated to be very laid back, humorous and as nicely as new all at once. greatest man speech 202377
2018年8月19日 09:30718042 466277Sweet web web site , super style and style , rattling clean and utilize genial . 981329
2018年8月15日 20:15595453 491717Does your internet site have a contact page? Im having trouble locating it but, Id like to send you an e-mail. Ive got some suggestions for your blog you may be interested in hearing. Either way, wonderful blog and I appear forward to seeing it develop over time. 760269
2018年8月15日 17:33测试下
2018年8月7日 04:20学习了
2017年7月1日 20:49防偷看小补丁,阻止以一切名义查水表!【秘密留言】
2016年5月9日 22:13只是主题中不输出那部分内容
2016年5月15日 14:12测试评论
2014年12月3日 19:25对内容网站来说,这个不太友好
2014年10月20日 21:57