纯代码实现WordPress评论后可见功能
很多时候,我们可以在很多网站或论坛中看到:“该文章需要评论后才能看到”,“本链接需要回复才能下载”等效果。
这样对于访客虽然有点麻烦,但对于站长或网站本身的流量是有很大的帮助的,同时也增强了文章的权重,有利于SEO。
代码是从本站主题中提取出来的。
如图效果:
一、首先将下面给的代码,加到你的主题的 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 = "wx@longtengwx.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');
二、然后就是短代码部分(《龙腾博客》添加了“评论可见”短代码功能,所以把下面两段代码中的“{}”大括号都改成“[]”中括号),编写文章时插入:
[reply]
{reply}评论可见的内容{/reply}
三、提醒内容也可以自定义,代码如下:
{reply notice="自定义提醒回复内容"}自定义提醒回复内容{/reply}
这段代码直接对博主显示而不需要评论![/reply]














