要直接在WordPress中执行此操作,可以使用以下方法:
<?php
$args = array(\'category\' => *term_id of category to disable comments for*);
$myposts = get_posts($args);
foreach($myposts as $post) {
$my_post = array(
\'ID\' => $post->ID,
\'comment_status\' => \'closed\'
);
wp_insert_post($my_post);
}
?>
get_posts() 将检索中指定类别的所有帖子
$args 签署人:
\'category\' => $term_id
wp_insert_post() 将允许您使用以下设置修改所述帖子以禁用评论:
\'comment_status\' => \'closed\'
您可能希望查看的文档页
get_posts() 和
wp_insert_post() for more information.
To ensure that no future posts under this category allow comments you could also use the save_post hook:
save_post 每当创建或更新帖子或页面时运行,可以从导入、帖子/页面编辑表单、xmlrpc或通过电子邮件发布。操作函数参数:post ID。
<?php
add_action(\'save_posts\', \'disable_comments\');
function disable_comments($post_id) {
$disabled_category = /* term_id of the category to disable comments for */
$category = get_the_category($post_id);
if($category->cat_ID == $disabled_category) {
$my_post = array(
\'ID\' => $post_id,
\'comment_status\' => \'closed\'
);
wp_insert_post($my_post);
}
}
?>
get_the_category() 将检索所选帖子的类别对象。访问documentation 了解更多有关get_the_category().如果你不知道什么是动作钩,你应该访问this page.