添加以下代码到function.php文件:



  1. <?php   

  2. function close_comments( $posts ) {   

  3.     if ( !is_single() ) { return $posts; }   

  4.     if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {   

  5.         $posts[0]->comment_status = 'closed';   

  6.         $posts[0]->ping_status    = 'closed';   

  7.     }   

  8.     return $posts;   

  9. }   

  10. add_filter( 'the_posts', 'close_comments' );   

  11. ?>  



代码中的数字“30”是指发表超过30天的日志将会关闭评论功能。
通过运行SQL语句也可以关闭日志评论功能,登录SQL数据库管理后台:



  1. UPDATE wp_posts SET comment_status = 'closed';       //关闭所有的日志评论功能;   

  2. UPDATE wp_posts SET comment_status = 'registered_only';     //只有注册用户才能发表评论;   

  3. UPDATE wp_posts SET comment_status = 'closed' WHERE post_date < '2011-01-01' AND post_status = 'publish';               // 关闭2011年01月01日之前发表的日志评论功能,可以设定成你想要的其它日期  



对数据库操作前应该先做好备份。


最后修改:2019 年 06 月 06 日
收藏不易,如果感觉对你有用,请随意打赏支持下