How do I assign a block template (.html) to a custom post type?

Question

I’m using WordPress 6.0 and I’ve created a child theme from TwentyTwentyTwo. I’ve created a custom post type using code like this:

register_post_type('foobar', [
   'public' => true,
   'show_in_rest' => true, // enable block editor
   // etc
]);

In my child theme, I’ve created the directory templates, and within it, a file named foobar.html. I’ve copied the contents of single.html from twentytwentytwo/templates/ to this folder and renamed it to foobar.html to make it match the custom post type.

If I open the site editor, and click “Templates”, I see these templates listed:

  • Single Post
  • Page
  • foobar
  • Home
  • Search
  • etc…

However, when I create a new post of the custom post type foobar using the WordPress admin interface, the default template is “Default template”, and not “foobar”. How do I change the default block template for a custom block type?

0
Flimm 1 month 2022-07-15T13:26:54-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse