• Resolved mooogle

    (@mooogle)


    Hello,

    I have a site that used to have a classic theme and a custom post type (registered with my own plugin). The type has some custom fields, and it appears in the admin panel, where I can easily add new posts. For the classic theme, I made a .php file to display the custom post type.

    Recently, I decided to switch to a block theme (Twenty Twenty-Three). It doesn’t use php files, but html templates. I wanted to use this guide to make the custom post template, but my custom post type doesn’t appear under the “+” button for adding new templates. ??

    Almost all the online guides for custom post types are about classic themes, not block themes, so I can’t really find any information about what’s wrong. Could there be something missing in the function for registering new post type? Here’s what it looks like now:

    register_post_type('my_custom_post',
      array(
        'labels' => array(
          'name' => __('My custom posts', 'textdomain'),
          'singular_name' => __('Custom post', 'textdomain'),
        ),
        'public' => true,
        'publicly_queryable' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'mypost' ),
        'supports' => array( 'title' ),
      )
    );
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi mooogle!

    For your custom post type to appear in the list to add templates it needs to have show_in_rest set to true. I tried it and with that, it shows like this:

    I hope this unblock you to continue with the guide. Let us know how it goes.

    Thread Starter mooogle

    (@mooogle)

    It worked, thank you! ??

    I’m stuck on another problem, though. I can’t find a way to display custom fields in the block template editor. Is there some code you can enter to make it work, like it was in the case of php files?

    I see there are some plugins for custom fields, but I’d rather not use a third party plugin if it can be done without one.

    Thread Starter mooogle

    (@mooogle)

    Now that I think about it, I can probably just add shortcodes for each custom field in my plugin. But I’m wondering if there’s some more “official” way of putting custom fields in a custom post template.

    Glad it worked!

    Regarding custom fields for your custom post type, I believe that will likely require more code. Options may be using shortcodes as you mention or meta_box. But a plugin will probably be the cleanest and easiest option. I suggest if you have further questions to mark this as solved and open new topic(s), so each specific case can discussed in detail.

    Thread Starter mooogle

    (@mooogle)

    Thanks, I guess I’ll just try to add shortcodes to my plugin. I’m marking this as solved!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Block theme template for a custom post type’ is closed to new replies.