首页 问答 正文

如何在WordPress中禁用评论功能

注册会员 / 悠然自学 / 2023-06-11/ 浏览 132 次

在WordPress中禁用评论功能有以下两种方法:

  1. 在后台设置中关闭评论功能
    进入WordPress后台,依次点击设置 -> 讨论,在这个页面中找到 “允许访问者在新文章中发表评论” 这一选项,取消勾选。这样就关闭了全站的评论功能。

  2. 使用代码禁用单篇文章的评论
    在编辑文章页面中找到 “讨论” 这个选项卡,取消勾选 “允许评论” 和 “允许回复”。如果要禁用多篇文章的评论,可以使用代码实现。示例代码如下:

// 禁用所有文章的评论功能
function disable_comments_post_type_support() {
    $post_types = get_post_types();
    foreach ($post_types as $post_type) {
        if(post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
}
add_action('admin_init', 'disable_comments_post_type_support');

// 隐藏评论和评论表单
function disable_comments_hide_comments_template() {
    if(is_singular()) {
        wp_redirect(home_url());
        exit;
    }
}
add_action('template_redirect', 'disable_comments_hide_comments_template'); 

以上就是WordPress中禁用评论功能的方法。

大家谈论
    我的见解