阿林陪你看世界自媒体, 一个90年后的草根站长!个人博客,专注互联网+的发展!QQ2227948465,重庆SEO,SEO,重庆SEO博客,重庆SEO服务!

怎么为WordPress添加一个隐藏内容,评论可见

未分类 阿林 121℃ 0评论

有时候我们在写文章的时候,往往不希望读者能够支持一下作者的良苦用心,下面的代码可以让读者在评论回复之后才能看见所隐藏的内容

添加代码

将下面的代码添加到主题的 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]

 效果演示

http://www.youhonglin.com/?p=2300


本文由 文章来源于网络如需要删除联系作者:http://www.youhonglin.com/2303.html

本站部分内容来自网络,如有侵权,请联系我们进行处理,转载本站文章请注明出处!
喜欢 (0)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

阿林陪你看世界