dun*_*140 7 php wordpress custom-taxonomy wordpress-gutenberg
我已经在 Wordpress 中注册了一个自定义分类法,但我无法弄清楚为什么它没有显示在标准的 Wordpress 帖子中,因为已经引入了 Gutenberg。我的意思是,在添加或编辑帖子时,它不会显示在文档侧栏中。'Categories' 和 'Tags' 也是如此,它们显然是标准分类法。
我已经确保 'show_in_rest' => true 存在是分类法注册,但它没有任何区别。
似乎他们正在部分注册,因为它们显示在主左侧菜单的“帖子”下,这表明它可能与古腾堡有关?
有任何想法吗?
// Register taxonomy
add_action( 'init', 'register_taxonomy_articles_element' );
function register_taxonomy_articles_element() {
$labels = array(
'name' => _x( 'Elements', 'articles_element' ),
'singular_name' => _x( 'Element', 'articles_element' ),
'search_items' => _x( 'Search Elements', 'articles_element' ),
'popular_items' => _x( 'Popular Elements', 'articles_element' ),
'all_items' => _x( 'All Elements', 'articles_element' ),
'parent_item' => _x( 'Parent Element', 'articles_element' ),
'parent_item_colon' => _x( 'Parent Element:', 'articles_element' ),
'edit_item' => _x( 'Edit Element', 'articles_element' ),
'update_item' => _x( 'Update Element', 'articles_element' ),
'add_new_item' => _x( 'Add New Element', 'articles_element' ),
'not_found' => _x( 'No Elements found', 'articles_element' ),
'new_item_element' => _x( 'New Element', 'articles_element' ),
'separate_items_with_commas' => _x( 'Separate Elements with commas', 'articles_element' ),
'add_or_remove_items' => _x( 'Add or remove elements', 'articles_element' ),
'choose_from_most_used' => _x( 'Choose from the most used elements', 'articles_element' ),
'menu_name' => _x( 'Elements', 'articles_element' )
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'show_ui' => true,
'show_tagcloud' => true,
'hierarchical' => true,
'rewrite' => true,
'query_var' => true
);
register_taxonomy( 'element', array('post'), $args );
}
Run Code Online (Sandbox Code Playgroud)
小智 12
由于古腾堡是基于 REST API 工作的,因此您需要为任何自定义帖子类型和分类法打开对 REST API 的支持。您需要show_in_rest = true向$args数组添加额外的密钥。您的完整代码应如下所示:
$args = array(
'labels' => $labels,
'public' => true,
'show_in_rest' => true, // add support for Gutenberg editor
'publicly_queryable' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'show_ui' => true,
'show_tagcloud' => true,
'hierarchical' => true,
'rewrite' => true,
'query_var' => true
);
Run Code Online (Sandbox Code Playgroud)