如何禁用 wordpress 中特定类别的评论?

how to disable the comments for specific category in wordpress?

如何禁用 Wordpress 中特定类别的评论表单。 我的意思是每个 post 用户将在这个类别中发布它不会有评论表单内容。

您可以创建两个模板文件,而不是在 functions.php 中执行此操作。第一个模板文件是您的标准主题模板,第二个模板文件是相同的,只是它不包含评论代码部分。

只需将没有注释代码块的模板文件命名为 category_{id}.php 并上传到您的主题文件夹。 ID 是您要禁用评论的类别的 ID。

此处有更多关于特定类别模板的信息https://developer.wordpress.org/themes/basics/template-hierarchy/#category

在此处了解有关评论模板的更多信息https://codex.wordpress.org/Function_Reference/comments_template

如果您仍想通过 functions.php 执行此操作,请参阅此博客 post http://spicemailer.com/wordpress/disable-hide-comments-posts-specific-categories/,其中使用以下代码片段

add_action( 'the_post', 'st_check_for_closed' );

function st_check_for_closed()
{

global $post;

$my_post_cat = wp_get_post_categories($post->ID);


$disabled_cat = array( "1", "3"); // this is he array of disabled categories. Feel free to edit this line as per your needs. 


$my_result = array_intersect($my_post_cat,$disabled_cat);

    if (empty ( $my_result ) ) 
                  {
        return; 
                   }

     else { 
           add_filter( 'comments_open', 'st_close_comments_on_category', 10, 2 );
           add_action('wp_enqueue_scripts', 'st_deregister_reply_js');

         }
}

      function st_deregister_reply_js() 
    {
    wp_deregister_script( 'comment-reply' );

    }


  function st_close_comments_on_category ($open, $post_id) 
    {
        $open = false;
    }