2015-07-12 69 views
0

如何禁用Wordpress中特定类别的评论表单。 我的意思是,用户将发布在这个类别的每一篇文章都不会有评论表单内容。如何禁用wordpress中特定类别的评论?

+2

你已经试过了什么?另外,您是否可以禁止在Wordpress编辑器中对每个帖子级别的评论? – terrorfall

+0

不幸的是,我找不到可用于禁用特定类别评论的功能...... –

+0

您是否可以禁止在Wordpress编辑器中针对每个帖子级别的评论? - >不,因为我想在开发层做到这一点。 –

回答

0

而不是在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中要做到这一点,看到这篇博客文章http://spicemailer.com/wordpress/disable-hide-comments-posts-specific-categories/它使用下面的代码snippet

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; 
    } 
+0

这是一个非常有用的想法。但我认为wordpress开发人员足够聪明,可以创建这个功能,我可以使用它来禁用特定类别,帖子或页面的评论。 –