最近一直有個小夥伴在群裡詢問 WordPress 網站如何設定文章內容評論後可見。正好小編沒事,索性也就寫一篇 WordPress 文章內容回覆後可見的教程 (PS:小編之前也寫過一篇類似的教程 《WordPress 文章部分內容登陸後可見》) 。現在來說說如何實現 WordPress 的文章內容評論後可見吧?其實實現起來很簡單,利用 WordPress 的短程式碼功能即可實現,程式碼如下:

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 = "www@weixiaoduo.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 檔案裡即可。

使用方法:

在編輯文章時插入以下程式碼即可隱藏內容

[reply] 我是被隱藏的內容 [/reply]

當然還可以自定義提示回覆資訊,短程式碼如下:

[reply notice="回覆後才顯示喲"] 我是被隱藏的內容 [/reply]

注意其中註釋了博主 E-mail 的地方改成你自己的 E-mail,這樣就可以設定成對站長不隱藏內容了。