之前曾經分享過一篇關於Easy2Hide插件的文章,文章介紹瞭如何使用該插件實現隱藏內容的方法與技巧。但是很多人反饋不喜歡使用插件去實現這些功能,那這篇文章就是介紹 WordPress 不用插件如何實現文章隱藏內容評論可見。

在當前主題的 functions.php 文件添加以下代碼:

< ?php
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '
< 建站網 blockquote> 温馨提示: 隱藏內容需要< 建站網 a href="#respond" title="點擊進行評論"> 回覆評論 後才能查看, 評論後請 < 建站網 a href="" title="點擊刷新"> 刷新 !< 建站網/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 = "[email protected]"; //把左面的郵箱換成博主 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');
?>

注:[email protected],將出現的建網站字眼刪掉!
編輯文章的時候,使用

温馨提示: 此處內容需要您評論本文後才能查看!

把要評論可見的內容包圍起來,如:

温馨提示: 此處內容需要您評論本文後才能查看!

文章會顯示:

温馨提示: 隱藏內容需要 回覆評論 後才能查看, 評論後請 刷新 !.

自定義的提示信息

自定義温馨提示的內容。

提醒:如果開通了評論審核,那麼內容需要評論審核通過後才可以看見!