[WordPress教程] WP文章评论或回复可见 -静鱼客栈

2021-10-12 0 797

现在来说说如何实现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,这样就可以设置成对站长不隐藏内容了。

 

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

静鱼客栈 学习中心 [WordPress教程] WP文章评论或回复可见 -静鱼客栈 https://wp.muooy.cn/10960.html

静鱼客栈的帅逼站长~

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务