如何在Wordpress中为特定类别禁用评论表单。我的意思是,用户将在此类别中发布的每个帖子都不会包含评论表单内容。
与其在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/,其中使用以下代码段
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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句