WordPress文章部分内容评论或回复可见

WordPress文章部分内容评论或回复可见,利用WordPress的短代码功能即可实现,代码如下:

function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '
<span style="color: red;">温馨提示:</span>此处内容需要<a title="评论本文" href="#respond">评论本文</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 = "service@dreamfy.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');

只需要将以上代码加入到当前使用的主题的functions.php文件里即可。博主E-mail的地方改成你自己的E-mail,这样就可以设置成对博主不隐藏内容了。

使用方法:

在编辑文章时插入以下代码即可隐藏内容:

[reply]需要被隐藏的内容[/reply]

当然还可以自定义提示回复信息,短代码如下:

[reply notice="回复后可见"]需要被隐藏的内容[/reply]
展开评论