WordPress文章部分内容评论/回复后可见

发布于 2021-02-26  1150 次阅读


话不多说,直接上代码

1、将以下代码添加到已启用主题的functions.php文件最后一个?>的前面。

//部分内容回复可见
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '
<span style="color: red;">温馨提示:</span>此处内容需要<a title="评论本文" href="#respond">评论本文</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 = "xm@ligzs.cn"; //博主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');

PS:将上面代码中的xm@ligzs.cn替换为站点管理员的邮箱地址,以实现管理员直接可见。

2、在编辑文章的时候,只需要插入短代码,在需要隐藏的部分内容中前后加上短代码即可。

[reply]我是被隐藏的内容[/reply]

PS:评论/回复后一定要刷新一下才能看到隐藏的内容哦,如果刷新后也无法看到,那肯定是你的评论需要管理员审核才行,只有审核通过后才能显示隐藏内容。

我们非常关注新冠疫情

[cov2019]

[cov2019map]

届ける言葉を今は育ててる
最后更新于 2021-02-26