现在的位置: 首页 > 技术分享 > WordPress > 正文

Wordpree文章隐藏内容回复可见如何实现?

2013年12月07日 WordPress ⁄ 共 1275字 ⁄ 字号 暂无评论

给Wordpress文章增加回复可见,既可防止采集党的肆意转载,保护原创;又可提高互动,增加留言,提高人气,不过Wordpress默认是不具备这样的功能,Wordpree文章隐藏内容回复可见如何实现?

其实很简单,我们可以通过在functions.php中添加代码来实现,具体代码如下:

//文章内容回复可见

function reply_to_read($atts, $content=null) {

extract(shortcode_atts(array("notice" => '<p class="reply-to-read"><b><span style="color: #FF0000;">温馨提示: 此处内容需要<a href="#respond" title="评论本文"><b>评论本文</b></a>后才能查看。</span></b></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 = "博主的Email"; //<span style="color: #0000ff;">博主Email</span>

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');

其中,博主的Email设置成自己的,博主可以不用回复就可见;另外,字体颜色可以自己修改默认为#FF0000红色。

使用方法

将需要隐藏的内容用[@reply]包裹,编辑文章时,添加:

[@reply]你希望评论回复可见的内容[@/reply]

或者

[@reply notice="自定义提醒回复内容"]自定义提醒回复内容[@/reply]

说明:使用时去掉@

给我留言

留言无头像?


×