本文實例講述了WordPress實現評論后可顯示內容中附件下載地址的方法。分享給大家供大家參考,具體如下:
最近在做一個項目的時候,有個需求就是希望WordPress網站文章內容里面附件可以評論后才可以下載。網絡上面查了會,發現這個功能不難實現,寫個簡單的函數就可以了。而且這樣也可以設置部分文章評論后可見。覺得這個功能應該挺多人有需要的,索性也就寫一篇wordpress文章內容回復后可見的教程?,F在來說說如何實現wordpress的文章內容評論后可見吧?其實實現起來很簡單,利用wordpress的短代碼功能即可實現,代碼如下:
代碼如下:
extract(shortcode_atts(array("notice" => '溫馨提示: 此處內容需要評論本文后才能查看.'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//對博主直接顯示內容
$admin_email = "xxx@aaa.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');
1.需要注意的是,要修改第8行的郵件為管理員的。如果你的網站使用了ajax免刷新提交評論,應該還需要修改第2行的提示文字,提示訪客評論后刷新頁面來查看隱藏內容。
2.編輯文章時,使用下面的簡碼:
【reply】評論可見的內容【/reply】
或者
【reply notice="自定義的提示信息"】評論可見的內容【/reply】
希望本文所述對大家基于wordpress的程序設計有所幫助。