现在来说说如何实现wordpress的文章内容评论后可见吧?其实实现起来很简单,利用wordpress的短代码功能即可实现,代码如下:
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '
<span style="color: red;">温馨提示:</span>此处内容需要<a title="评论本文" href="#respond" rel="external nofollow" >评论本文</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 = "www@ruikeedu.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,这样就可以设置成对站长不隐藏内容了。