给WordPress加个评论关闭时间提示

WordPress192.8K阅读模式

给WordPress加个评论关闭时间提示-图片1

为了阻止垃圾评论,我们可以进入WP后台 → 设置 → 讨论,在讨论设置面勾选“自动关闭发布 14天后的文章上的评论”,让之前较早发表的文章自动关闭评论。

如果再给读者用户一个什么时候关闭评论的时间提示,是不是更加人性化。

将下面的代码添加到当前主题functions.php文件中:

  1. function topic_closes_in() {
  2.     global $post;
  3.     if ($post->comment_status == 'open') {
  4.         $close_comments_days_old = get_option( 'close_comments_days_old' );
  5.         $expires = strtotime"{$post->post_date_gmt} GMT" ) +  $close_comments_days_old * DAY_IN_SECONDS;
  6.         printf( '提示:本文评论将于%s后自动关闭!',  human_time_diff( $expires ));
  7.     }
  8. }

调用函数

  1. <?php topic_closes_in(); ?>

添加到主题评论模板comments.php的适当位置即可。

给WordPress加个评论关闭时间提示-图片2

另外,这个自动关闭评论函数 human_time_diff 保存在wp-includes/formatting.php文件中。

源代码:http://wpengineer.com/2692/inform-user-about-automatic-comment-closing-time/

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
  • WordPress
  • Wordpress教程
  • WordPress主题
  • functions.php
评论  19  访客  19
    • Win7en乐园
      Win7en乐园 7

      非常实用的功能,没有什么好说的,加油,鸟哥!

      • 网赚
        网赚 2

        这个给力了

        • 励志语录
          励志语录 6

          不错的功能

          • 女装品牌大全
            女装品牌大全 1

            文章不错支持一下

            • Win7en乐园
              Win7en乐园 7

              @我爱动感单车网 ,美女加油吧

              • 动感单车网
                动感单车网 7

                “自动关闭发布 14天后的文章上的评论”,我个人觉得 :razz: :没什么必要关闭这个!

                • boke112导航
                  boke112导航 4

                  这个功能还是挺实在的

                  • 墨丶水瓶
                    墨丶水瓶 4

                    有段时间没来了,鸟哥的网站变成这样了 ?

                    • yearliny
                      yearliny 0

                      话说通过这个方法避免垃圾评论太过粗暴了,也阻止了很多正常评论。

                      • 明月登楼的博客
                        明月登楼的博客 5

                        嘿嘿,可以评论了,一个小意外!

                        • 孤独行者
                          孤独行者 0

                          路过

                        匿名

                        发表评论

                        匿名网友
                        :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

                        拖动滑块以完成验证