admin管理员组

文章数量:1277580

Whenever I attempt to define a level other than H2 for 'core/heading' in any block template, upon reload of the page editor, the block gets a "This block contains unexpected or invalid content" error, and when the "Attempt Block Recovery" button is clicked, the block level is changed to level 2. Why would I not seem to be able to use any other level than 2 in a block template?

Example template excerpt:

<?php
$template = [
  [
        'core/heading', 
        [
          'level' => '2',
            'content' => 'Directions',
            'className' => 'block-header-directions',
        ],
    ],
  [
        'core/heading', 
        [
          'level' => '3',
            'placeholder' => 'Subheading',
            'className' => 'block-subheader-directions has-blue-alt-color',
        ],
    ],
  [
        'core/list', 
        [
          'placeholder' => 'List item',
          'ordered' => true,
          'className' => 'block-list-directions',
        ],
  ],
];

In the above example, the block with the placeholder "Subheading" causes that chain of events.

I thought initially that this was potentially some enforcement of semantics (that Gutenberg wanted to see H1, H2, H3, etc. in the correct order in the content), but it occurs, as in the case above, even when the heading levels are in a completely sematic order in the content.

(Potentially related, as the forced H2 problem seems to extend to other editor areas as well: I've added a block style to 'core/heading', so the block editor now displays previews of the styles. But no matter which heading level is selected, only the preview for the H2 heading is displayed.)

Whenever I attempt to define a level other than H2 for 'core/heading' in any block template, upon reload of the page editor, the block gets a "This block contains unexpected or invalid content" error, and when the "Attempt Block Recovery" button is clicked, the block level is changed to level 2. Why would I not seem to be able to use any other level than 2 in a block template?

Example template excerpt:

<?php
$template = [
  [
        'core/heading', 
        [
          'level' => '2',
            'content' => 'Directions',
            'className' => 'block-header-directions',
        ],
    ],
  [
        'core/heading', 
        [
          'level' => '3',
            'placeholder' => 'Subheading',
            'className' => 'block-subheader-directions has-blue-alt-color',
        ],
    ],
  [
        'core/list', 
        [
          'placeholder' => 'List item',
          'ordered' => true,
          'className' => 'block-list-directions',
        ],
  ],
];

In the above example, the block with the placeholder "Subheading" causes that chain of events.

I thought initially that this was potentially some enforcement of semantics (that Gutenberg wanted to see H1, H2, H3, etc. in the correct order in the content), but it occurs, as in the case above, even when the heading levels are in a completely sematic order in the content.

(Potentially related, as the forced H2 problem seems to extend to other editor areas as well: I've added a block style to 'core/heading', so the block editor now displays previews of the styles. But no matter which heading level is selected, only the preview for the H2 heading is displayed.)

Share Improve this question asked Oct 31, 2021 at 1:00 StudioAlStudioAl 4941 gold badge3 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

A new day, and refreshed Google-fu powers provided the simple answer: The level attribute needs to be an integer, not a a string.

[
  'core/heading', 
  [
     'level' => 3,
     'placeholder' => 'Subheading',
     'className' => 'block-subheader-directions has-blue-alt-color',
  ],
]

本文标签: Creating Block templates with 39coreheading39 other than level 2