How to create a Gutenberg block template

alvinarichard

New Member
Registering Gutenberg Blocks
In the initial step we create custom blocks templates that can be used to build pre-populated blocks. Add the following code to the functions.php.

add_action( 'init', function() {
$args = array(
'public' => true,
'label' => 'News',
'show_in_rest' => true,
'template_lock' => 'all',
'template' => array(
array( 'core/paragraph', array(
'placeholder' => 'Breaking News',
) ),
array( 'core/image', array(
'align' => 'right',
) ),
),
);
register_post_type( 'news', $args );
} );

For adding custom block to this template, use the ‘template’ sub-array.

'template' => array(
array( 'core/heading', array( 'level' => '4', 'content' => 'Heading' ) ),
array( 'core/paragraph' ),
)

If you still find any issue you can go through this reference Gutenberg block templates
 
Top