WordPress自定义文章类型支持古腾堡编辑器(Gutenberg)的方法

WordPress教程 1507

WordPress 5.0版本之后启用Gutenberg编辑器作为默认文章编辑器,但是发现创建的自定义文章类型在添加文章时,使用的还是TinyMCE编辑器,查找资料后发现,要让自定义文章类型支持古腾堡编辑器,还需要在register_post_type中添加show_in_rest参数,据了解是大致是因为Gutenberg编辑器必须利用REST API进行更新和更改,总之问题是解决了。

代码如下:

1
2
3
4
5
6
7
8
9
10
$args = array(
    'labels'        => $labels,
    'description'   => '产品列表',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array('title','editor','thumbnail'),	//supports中需要有editor	
    'show_in_rest'  => true,//增加的参数
    'has_archive'   => true
);
register_post_type( 'product_type', $args );
WordPress官网对参数的说明:

‘show_in_rest’
(bool) Whether to include the post type in the REST API. Set this to true for the post type to be available in the block editor.

附:

wordpress创建自定义文章类型教程

精品推荐: