LoginSignup
0
0

More than 5 years have passed since last update.

カスタム投稿タイプ時 The block "***" must have a registered category. エラー

Posted at

症状

Gutenbergのカスタムブロックとカスタムカテゴリーを作った。
デフォルトの投稿だとGutenbergエディタが正しく起動するのに、カスタム投稿タイプから投稿しようとするとGutenbergエディタが起動せず真っ白になり、ブラウザコンソールにThe block "***" must have a registered category.のエラーが表示される。

同じエラーになる人少なそうだが30分ぐらいハマってたのでメモ。

原因と解決方法

Gutenbergのカスタムカテゴリーを作る際、
https://wordpress.org/gutenberg/handbook/designers-developers/developers/filters/block-filters/#managing-block-categories

my-plugin.php
function my_plugin_block_categories( $categories, $post ) {
    if ( $post->post_type !== 'post' ) {
        return $categories;
    }
    return array_merge(
        $categories,
        array(
            array(
                'slug' => 'my-category',
                'title' => __( 'My category', 'my-plugin' ),
                'icon'  => 'wordpress',
            ),
        )
    );
}
add_filter( 'block_categories', 'my_plugin_block_categories', 10, 2 );

をコピペしていたが、この思考停止コピペがよくなくて、このうちの

my-plugin.php
    if ( $post->post_type !== 'post' ) {
        return $categories;
    }

の3行部分が不要だった。この3行削除し解決。
if (デフォルトの投稿じゃない時) {カスタムカテゴリ追加せずreturn} ってことになってので。

単純なミスであるが、WordPressも英語ドキュメントも浅い理解と雰囲気コピペで実装してたためハマった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0