2025-03-24

在 WordPress 後臺 設置-討論,我們可以設置自動關閉發佈瞭多少天後的文章迴響:

2014-11-20_095300_wpdaxue_com

 

但是用戶並不知道文章的關閉迴響的時間,我們可以在迴響表單上方添加一個提示:

2014-11-20_100020_wpdaxue_com

要實現這個功能,隻需要將下面的代碼添加到當前主題的 functions.php 即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * WordPress 告知用戶迴響關閉的時間
 * https://www.wpdaxue.com/automatic-comment-closing.html
 */
add_action( 'comment_form_top', 'wpdx_comment_closes_in' );
function wpdx_comment_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 後關閉)', 'domain' ),  human_time_diff( $expires ));
    }
}

/**
* WordPress 告知用戶迴響關閉的時間
* https://www.wpdaxue.com/automatic-comment-closing.html
*/
add_action( ‘comment_form_top’, ‘wpdx_comment_closes_in’ );
function wpdx_comment_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 後關閉)’, ‘domain’ ), human_time_diff( $expires ));
}
}

參考資料:http://wpengineer.com/2692/inform-user-about-automatic-comment-closing-time/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *