Skip to content

Instantly share code, notes, and snippets.

@zgordon
Last active February 15, 2018 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zgordon/b9e31a7d8a0e1dc0d356b92360512096 to your computer and use it in GitHub Desktop.
Save zgordon/b9e31a7d8a0e1dc0d356b92360512096 to your computer and use it in GitHub Desktop.
This shows how to create a custom post type and assign it a block template. This would likely go in your functions.php file.
<?php
function mytheme_create_post_type() {
register_post_type( 'mytheme_event',
[
'labels' => [
'name' => __( 'Events' ),
'singular_name' => __( 'Event' )
],
'public' => true,
'show_in_rest' => true,
// Optionally lock templates from further changes
// Change to 'insert' to allow adding other blocks, but lock defined blocks
'template_lock'] = 'all';
// Set block template
'template' => [
[
// Example of including a core image block
// Optional alignment setting
'core/image', [
'align' => 'left',
]
],
[
// Example of including a core paragraph block
// Optional alignment placeholder setting
'core/paragraph', [
'placeholder' => 'The only thing you can add',
'align' => 'right',
]
],
[
// Example of including a custom block
// Optional placeholder setting
'custom/your-blocks', [
'placeholder' => 'Custom placeholder',
]
]
]
]
);
}
add_action( 'init', 'mytheme_create_post_type' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment