最近在做一個(gè)項(xiàng)目的時(shí)候,,有個(gè)需求就是希望WordPress網(wǎng)站文章內(nèi)容里面附件可以評(píng)論后才可以下載,。網(wǎng)絡(luò)上面查了會(huì),發(fā)現(xiàn)這個(gè)功能不難實(shí)現(xiàn),,寫個(gè)簡(jiǎn)單的函數(shù)就可以了,。而且這樣也可以設(shè)置部分文章評(píng)論后可見。覺(jué)得這個(gè)功能應(yīng)該挺多人有需要的,,索性也就寫一篇wordpress文章內(nèi)容回復(fù)后可見的教程?,F(xiàn)在來(lái)說(shuō)說(shuō)如何實(shí)現(xiàn)wordpress的文章內(nèi)容評(píng)論后可見吧?其實(shí)實(shí)現(xiàn)起來(lái)很簡(jiǎn)單,,利用wordpress的短代碼功能即可實(shí)現(xiàn),,代碼如下:
function reply_to_read($atts, $content=null) { extract(shortcode_atts(array("notice" => '溫馨提示: 此處內(nèi)容需要評(píng)論本文后才能查看.'), $atts)); $email = null; $user_ID = (int) wp_get_current_user()->ID; if ($user_ID > 0) { $email = get_userdata($user_ID)->user_email; //對(duì)博主直接顯示內(nèi)容 $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行的郵件為管理員的,。如果你的網(wǎng)站使用了ajax免刷新提交評(píng)論,,應(yīng)該還需要修改第2行的提示文字,提示訪客評(píng)論后刷新頁(yè)面來(lái)查看隱藏內(nèi)容,。
2.編輯文章時(shí),,使用下面的簡(jiǎn)碼:
【reply】評(píng)論可見的內(nèi)容【/reply】
或者
【reply notice="自定義的提示信息"】評(píng)論可見的內(nèi)容【/reply】