| 大家知道,WordPress 是自帶文章釋出一定時間之後自動關閉評論的功能。如果要想利用評論數量來通知評論的開閉,要如何實現呢?雖然這個功能侷限性比較大,但是可能還是會有站點需要的。
開啟當前主題的 functions.php 檔案,新增
1
2
3
4
5
6
7
8
9
10
11
12
|
//評論超過一定數量關閉評論
function disable_comments( $posts ) {
if ( !is_single() ) {
return $posts;
}
if ( $posts[0]->comment_count > 50 ) {
$posts[0]->comment_status = 'disabled';
$posts[0]->ping_status = 'disabled';
}
return $posts;
}
add_filter( 'the_posts', 'disable_comments' );
|
其中,50 可以修改成自己需要的數字,表示一篇文章的評論如果超過這個數值,則自動關閉評論。
|