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

添加代码

将下面的代码添加到主题的 functions.php 文件:

//部分内容评论可见
function reply_to_read($atts, $content = null) {
 extract(shortcode_atts(array("notice" => '

注意:本段内容须成功“回复本文”后“刷新本页”方可查看!

') , $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.dungei.com/2604.html

你可能感兴趣的:(站长教程,wordpress)