Learn in this tutorial how to create a block template for Custom Post Type posts in a classic WordPress theme.
If you haven’t yet taken the step to using block themes (FSE) but want to take advantage of the block editor’s functionality in a classic theme, block templates are a great option to do so.
For example, WordPress allows you to generate a predefined block template and associate it with a content type.
This way, if you or your client always use the same structure in the entries of a Custom Post Type, you just have to create a template in which to enter information.
Better with an example?
Imagine that the entries of a CPT always have the following structure:
- Introduction paragraph
- Image | Header
- Paragraph with content
In this case you have the option of creating a template with the necessary blocks so that every time you generate a new entry the author only has to worry about adding the content.
Sounds good, doesn’t it?
This would be the result of adding a new entry:
Find out how to do it below.
Snippet to create a block template for CPT entries in a classic WordPress theme
Add the following code at the end of functions.php or in your functionality plugin:
In the code you use the function
get_post_type_object to obtain the content type object of your choice, in this case the entries of the CPT course (you can modify it for the one you want) and you indicate the blocks that you want the template assigned to that content type to contain.
As you can see, block templates allow you to use nested blocks.
In this case, you use a column block and inside other blocks, such as image, header or list. But you can use other blocks that allow you to nest content, such as group or background.
To add new blocks you have to know the “name” of the block. For example, to insert the paragraph block it is:
Here you have the list with all the native WordPress blocks:
You can also use blocks created by other plugins, you just have to know the name with which they have been registered. You can take a look at the tutorial on how to get a list of all the blocks registered in a WordPress installation.
Once you know the internal name of the blocks you want to use, you have the possibility to add content in the blocks using the block attributes. For example,
'content' or insert filler content by typing
And again I invite you to modify the text of both according to your needs. Please note that not all blocks accept both options.
You can check the attributes supported by each native block in the official documentation.
Note: by default, the header block adds a H2, if you want to introduce a header H3, H4, H5…etc you can do it in the following way:
[ 'core/heading', [ 'level' => 3, 'content' => 'H3 header', ] ],
If you have a defined structure that is repeated in the entries of a Custom Post Type, creating a block template is a fantastic way to save time
Even more so if you do it for your client, since it considerably facilitates the use of the web.
And now you know how to do it in a simple way. Just use this code as a starting point and adjust it to the needs of the project.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉