Wypo Logo Header@2x
Blog / Developer / Gutenberg / Creating a layout template with Gutenberg

Creating a layout template with Gutenberg

Developer Gutenberg
Time to read : 5 minutes

Publication date: 24 October 2024

Gutenberg offers an intuitive drag-and-drop content creation interface, enabling real-time customization. By defining block templates for your CPT, you guide users in structuring their content, reducing errors and ensuring consistent formatting.

Defining the template in a custom post type

You can also define a predefined block template that the user will see when creating new content for this CPT. This is done with the template argument when you register the CPT.

Here’s an example where you define a block template for the CPT portfolio:

PHP

Explanation of options :

  • template: An array containing the default blocks that will be inserted when the user creates new content for this CPT.
  • template_lock: Can be set to 'all' or 'insert' :
    • 'all': The user can neither modify nor delete defined blocks.
    • 'insert': The user can modify the content of blocks, but not delete them or add new ones.

Create a template on an existing CPT

You can use WordPress filters to add block templates to an existing CPT. To do this, you can use the register_post_type_args filter to add a block template when the CPT is registered.

Example of code to add to your functions.php file:

PHP
Last 30 days : 1
Total : 152