我为特定帖子创建了一个新的自定义帖子类型。我的新自定义帖子类型中的帖子在默认情况下将注释设置为“关闭”。我需要在默认情况下将注释设置为“开”。
在我的functions.php
文件我有:
\'supports\' => array(\'editor\',\'comments\')
以及
function default_comments_on( $data ) {
if( $data[\'post_type\'] == \'registro\' && $data[\'post_status\'] == \'auto-draft\' ) {
$data[\'comment_status\'] = 1;
}
return $data;
}
add_filter( \'wp_insert_post_data\', \'default_comments_on\' );
但默认情况下,它没有将框标记为注释。有什么建议吗?
我的默认帖子,不显示评论的元框,默认情况下允许评论。我想用我的新自定义帖子类型做到这一点。我的意思是:默认情况下,不显示metabox并打开注释。
登记册:
function create_post_type_registro() {
/**
* Labels customizados para o tipo de post
*
*/
$labels = array(
\'name\' => _x(\'Registros\', \'post type general name\'),
\'singular_name\' => _x(\'Registro\', \'post type singular name\'),
\'add_new\' => _x(\'Adicionar novo\', \'film\'),
\'add_new_item\' => __(\'Adicionar novo registro\'),
\'edit_item\' => __(\'Editar registro\'),
\'new_item\' => __(\'Novo registro\'),
\'all_items\' => __(\'Todos os registros\'),
\'view_item\' => __(\'Ver registro\'),
\'search_items\' => __(\'Procurar registros\'),
\'not_found\' => __(\'Nenhum registro encontrado\'),
\'not_found_in_trash\' => __(\'Nenhum registro encontrado na lixeira\'),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Registros\'
);
/**
* Registamos o tipo de post registro através desta função
* passando-lhe os labels e parâmetros de controle.
*/
register_post_type( \'registro\', array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'has_archive\' => \'registros\',
\'rewrite\' => array(
\'slug\' => \'registros\',
\'with_front\' => false,
),
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'editor\',\'comments\')
)
);
SO网友:cybmeta
前面的答案对我不适用,我认为它对大多数人都不适用。即使它适用于某些人,它也会覆盖整个网站的WordPress配置,而不是特定于所需的自定义帖子类型。
对我来说,在自定义帖子类型中启用评论并删除评论状态元框的正确方法如下(假设帖子类型已注册为支持评论):
add_action(\'admin_menu\', \'cyb_remove_meta_boxes\');
function cyb_remove_meta_boxes() {
remove_meta_box(\'commentstatusdiv\', \'post-type-here\', \'normal\');
}
add_filter( \'wp_insert_post_data\', \'cyb_comments_on\' );
function cyb_comments_on( $data ) {
if( $data[\'post_type\'] == \'post-type-here\' ) {
$data[\'comment_status\'] = "open";
}
return $data;
}
或者,如果我们想让编辑关闭评论,也许更好:
add_action(\'admin_menu\', \'cyb_remove_meta_boxes\');
function cyb_remove_meta_boxes() {
if( ! current_user_can( \'edit_others_posts\' ) ) {
remove_meta_box(\'commentstatusdiv\', \'post-type-here\', \'normal\');
add_filter( \'wp_insert_post_data\', \'cyb_comments_on\' );
}
}
function cyb_comments_on( $data ) {
if( $data[\'post_type\'] == \'post-type-here\' ) {
$data[\'comment_status\'] = "open";
}
return $data;
}
此外,我认为法典是错误的,因为它说
comment_status
设置为默认注释状态。这意味着,如果自定义帖子类型支持注释,并且WordPress配置中的默认注释状态为新帖子打开,则默认情况下,任何新帖子都应打开注释,但如果删除注释状态元框,则不会出现这种情况。这就是为什么需要覆盖
comment_status
插入立柱时。