一般的朋友都希望评论越多越好,但也有些朋友希望限制评论数量。下面倡萌分享下超过限制天数后自动关闭WordPress文章的评论功能的方法。
访问后台 >设置>讨论,参考下图设置:

你可以直接忽视下文内容啦!
将下面的代码添加到主题的 functions.php 文件即可:
//关闭超过N天的旧文章评论
function close_comments( $posts ) {
if ( !is_single() ) { return $posts; }
if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->ping_status = 'closed';
}
return $posts;
}
add_filter( 'the_posts', 'close_comments' ); |
//关闭超过N天的旧文章评论
function close_comments( $posts ) {
if ( !is_single() ) { return $posts; }
if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->ping_status = 'closed';
}
return $posts;
}
add_filter( 'the_posts', 'close_comments' );
上面的代码的作用是,文章发布超过30天后,就自动关闭这篇文章的评论功能。你可以根据自己的需要,修改第四行 的 30 为所需天数。
该方法在 WP 3.5.1 中测试有效。
参考资料:http://perishablepress.com/wordpress-tip-disable-comments-in-old-posts-via-php/
来源:
https://www.wpdaxue.com/disable-comments-on-old-posts.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33216.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57