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

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

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

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

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

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

调用函数

<?php topic_closes_in(); ?>

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

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

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索