海外CN2直连服务器
腾讯云优惠|主机测评网!

WordPress无需插件实现隐藏内容回复可见

许多网站发布的资源比较有价值但是活跃度就是非常低,为了提供互动性给网站添加隐藏内容回复可见的功能是非常有必要的。

实现方法:给文章添加判断函数,默认将内容转换为隐藏代码,当检测到用户评论了该文章则输出内容。

将下列代码插入到主题的functions.php中:

//文章内容回复可见  
add_shortcode('reply', 'reply_to_read');  
   
function reply_to_read($atts, $content=null) {  
extract(shortcode_atts(array("notice" => '<p>温馨提示:此处内容需要<a href="#respond" title="评论本文">评论本文</a>后才能查看。</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 = "admin@itbulu.com"; //<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 (emptyempty($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;  
}  
} 

代码部分添加到功能页面,修改下里面的博主email项,填写为自己的邮箱即可。

然后我们需要在合适的部分隐藏:

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

更多WordPress的建站教程请移至:https://www.idc1680.com/webqd/wordpress

赞(0)
未经允许不得转载:雪花测评 » WordPress无需插件实现隐藏内容回复可见
分享到: 更多 (0)

国内建站推荐腾讯云轻量级应用服务器,开箱即用。最新活动:点我进入