WordPress批量关闭或打开已发布文章的评论功能
本文最后更新于 661 天前,文中的信息可能已经有所变化。如有误,请留言反馈。

WordPress的评论功能可以加强博主与访客的交流,但如何灵活的管理评论?你可能需要这些神奇的代码:

  • 有特殊情况时需要批量关闭已发布文章的评论功能,那么你可能会使用到以下代码:
/**
* WordPress一键关闭/开启评论功能
*/
function close_open_comments( $posts ) {
$postids = array('666','777');
if ( !empty( $posts ) && is_singular() && !in_array($posts[0]->ID,$postids) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->post_status = 'closed';
}
return $posts;
}
add_filter( 'the_posts', 'close_open_comments' );

*$postids = array(‘666′,’777’);里面的666和777分别为需要保留评论的页面ID,也就是说ID为666和777的文章时可以评论的,可以按照需求修改。

  • 限制单篇文章评论总数,达到一定数量后关闭评论:
/**
* 当评论达到一定数量后自动关闭WordPress文章的评论功能
*/
function lxtx_disable_comments( $posts ) {
if ( !is_single() ) { 
return $posts; 
}
if ( $posts[0]->comment_count > 100 ) {
$posts[0]->comment_status = 'disabled';
$posts[0]->ping_status = 'disabled';
}
return $posts;
}
add_filter( 'the_posts', 'lxtx_disable_comments' );

*$posts[0]->comment_count > 100 这里是控制评论条数的,大于100条自动关闭评论功能,按需添加及修改。

  • 超过限制天数或小时数后自动关闭WordPress文章的评论功能
/**
* 超过限制天数或小时数后自动关闭WordPress文章的评论功能
*/
function lxtx_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', 'lxtx_close_comments' );

*文章发布超过 30 天后,就自动关闭这篇文章的评论功能。可以按照自己需求修改30 * 24 * 60 * 60参数。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇