WordPress – 为文章增加评论才可查看相关隐藏内容的代码
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:WordPress – 为文章增加评论才可查看相关隐藏内容的代码
原文链接:https://www.stubbornhuang.com/811/
发布于:2020年05月05日 19:19:34
修改于:2020年05月05日 19:32:19

1 比较普通的CSS的代码
在主题的function.php中加入以下代码:
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<p class="reply-to-read" style="border-width: 1px 1px 1px 1px;border-color: #F2F2F2;line-height: 150%;"><blockquote><font color="#ff0000"><b>温馨提示</b></font>: 隐藏内容需要<a href="#respond" title="点击进行评论"> 回复评论 </a>后才能查看, 评论后请 <strong><a href="javascript:location.reload()" title="点击刷新"> 刷新 !</a></strong>.</blockquote></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 = "xxxxxxx@xx.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');
2 增加了红点边框的CSS的代码
在主题的function.php中加入以下代码:
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<p class="reply-to-read" style="text-align:center; border:2px solid #f00; border-style:dotted; border-radius:4px; padding:5px; margin:10px;"><strong style="color: red;">温馨提示:</strong>为了避免资源链接被和谐,此处内容需要您<strong><a href="#respond" title="点击进行评论"> 回复评论 </a></strong>后才能查看, 评论后请 <strong><a href="javascript:location.reload()" title="点击刷新"> 刷新!</a></strong></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 = "xxxxxxx@xx.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');
3 修改CSS样式
大家可以根据自己的需要修改上述代码extract语句中css自己定制化的css样式。
样式示例:
4 使用方法

5 注意的点
如果站点在后台设置了评论审核,那么游客评论之后需要站点管理员审核评论通过之后刷新页面才会看到隐藏内容,所以如果要使用该功能建议关闭评论审核功能。
当前分类随机文章推荐
- WordPress - wp_login_url函数详解 阅读354次,点赞0次
- WordPress - 文章自定义字段post_meta添加、更新、删除、获取操作 阅读259次,点赞0次
- WordPress - get_post_type():获取当前文章或者给定文章类型 阅读1366次,点赞0次
- WordPress - 站点底部显示站点运行时间 阅读3390次,点赞0次
- WordPress - get_footer函数,加载主题底部页脚footer模板 阅读166次,点赞0次
- WordPress - 修改管理后台登录地址,防止恶意爆破 阅读1980次,点赞0次
- WordPress - $Post WP_Post对象的属性 阅读1437次,点赞0次
- WordPress - 使用Cravatar替换Gravatar提供头像服务 阅读203次,点赞0次
- WordPress - 添加网页三角网粒子背景 Canvas-nest.js 阅读2593次,点赞0次
- WordPress - 禁用XML-RPC接口,禁止访问xmlrpc.php,避免DDOS攻击,防止暴力破解 阅读2046次,点赞0次
全站随机文章推荐
- 资源分享 - Vulkan学习指南 , Learning Vulkan 中文版PDF下载 阅读87次,点赞0次
- 资源分享 - Tricks of the 3D Game Programming Gurus - Advanced 3D Graphics and Rasterization 英文高清PDF下载 阅读720次,点赞0次
- 资源分享 - Practical Linear Algebra - A Geometry Toolbox , Fourth Edition 英文高清PDF下载 阅读331次,点赞0次
- C++ - 获取当前进程内存使用情况 阅读4174次,点赞4次
- 旋转矩阵与四元数的转换 阅读3484次,点赞0次
- Modern OpenGL从零开始 - 从茫茫多的OpenGL第三方库讲起 阅读2483次,点赞1次
- Python3爬虫 - 下载反盗链图片的方式 阅读2067次,点赞1次
- 资源下载 - OpenGL着色语言OpenGL橙宝书PDF中文版下载 阅读5959次,点赞5次
- Duilib - RichEdit和List等控件增加垂直滚动条vscrollbar和水平滚动条hscrollbar 阅读499次,点赞2次
- TensorRT - 安装TensorRT工具Polygraphy 阅读1244次,点赞0次
评论
144