无法将默认评论添加到自定义帖子类型

时间:2016-10-24 作者:aido14

我正在尝试向我的自定义帖子类型添加取消评论。

我在我的支持论点中添加了:

\'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'tags\',\'post-formats\',\'comments\',\'revision\'),
我还尝试了一些类似的功能:

function default_comments_on( $data ) {
if( $data[\'post_type\'] == \'your_custom_post_name\' ) {
    $data[\'comment_status\'] = 1;
}

return $data;
}
add_filter( \'wp_insert_post_data\', \'default_comments_on\' );
但似乎什么都不管用。以下是我的自定义帖子类型:

function spark_register() {
$labels = array(
    \'name\' => _x(\'Spark Garden\', \'post type general name\'),
    \'singular_name\' => _x(\'Spark\', \'post type singular name\'),
    \'add_new\' => _x(\'Add New Spark\', \'sparknz\'),
    \'add_new_item\' => __(\'Add New Spark\'),
    \'edit_item\' => __(\'Edit Spark\'),
    \'new_item\' => __(\'New Spark\'),
    \'view_item\' => __(\'View Spark\'),
    \'search_items\' => __(\'Search Sparks\'),
    \'not_found\' =>  __(\'Nothing found\'),
    \'not_found_in_trash\' => __(\'Nothing found in Trash\'),
    \'parent_item_colon\' => \'\'
  );
 $args = array(
    \'labels\' => $labels,
    \'public\' => true,
    \'has_archive\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => true,
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,
    \'menu_position\' => 8,
    \'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'tags\',\'post-  formats\',\'comments\',\'revision\'),
          \'taxonomies\' => array(\'post_tag\'),
); 
register_post_type( \'spark\' , $args );
}


add_action(\'init\', \'spark_register\');
我已经尝试添加到“Twenty16”并激活,但默认情况下仍不会显示注释。我已选中它以允许在我的设置中添加评论,它确实显示在我的常规“帖子”帖子类型中,但在我的cutom帖子类型中,讨论复选框未选中,默认情况下不显示评论?

提前感谢。。

1 个回复
SO网友:aido14

经过一番搜寻,我发现了这一点。对于任何想知道的人:

add_filter( \'comments_open\', \'my_comments_open\', 10, 2 );

function my_comments_open( $open, $post_id ) {

$post = get_post( $post_id );

if ( \'spark\' == $post->post_type )
    $open = true;

return $open;
}