New Post Type template using blockeditor

先读这个 Post Type – 笔记

To enable the block editor (Gutenberg) for your custom post type “book” (or whatever your post type is), you need to modify the register_post_type arguments within your functions.php file or custom plugin. Specifically, you need to add or modify the supports argument.

Here’s how you can do it:

1. Modify the supports Argument:

In your create_book_post_type() function (or the function where you register your custom post type), locate the supports array.

  • If supports doesn’t exist, create it.
  • If supports exists, add or ensure that 'editor' is included.

Here’s the modified code snippet:

PHP

function create_book_post_type() {
    // ... (Your labels and other args) ...

    $args = array(
        // ... (Your other arguments) ...
        'supports'            => array( 'title', 'editor', 'thumbnail', 'custom-fields' ), // Ensure 'editor' is present
        // ... (Your other arguments) ...
    );
    register_post_type( 'book', $args );
}
add_action( 'init', 'create_book_post_type', 0 );

Explanation:

  • The 'editor' element within the supports array tells WordPress to enable the block editor for your custom post type.
  • By default, if the 'editor' support is not declared, then WordPress will default to the classic editor.

2. Enable Template Support (Optional but Recommended):

If you want to use block templates (predefined block layouts) for your “book” post type, you can also add 'template' to the supports array:

PHP

$args = array(
    // ...
    'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'template' ),
    // ...
);

3. Create Block Templates (Optional):

If you enabled template support, you can create block templates to define the initial content and layout for new “book” posts. Here’s how:

  • Within your theme’s functions.php file or a custom plugin, use the register_block_template function.

PHP

function register_book_template() {
    $post_type_object = get_post_type_object( 'book' );
    $post_type_object->template = array(
        array( 'core/paragraph', array(
            'placeholder' => 'Enter book description here...',
        ) ),
        array( 'core/image', array(
            'align' => 'center',
        )),
        array( 'core/heading', array(
            'placeholder' => 'Additional Information',
            'level' => 3,
        )),
    );
}
add_action( 'init', 'register_book_template' );
  • This code defines a template with a paragraph, an image, and a heading. When you create a new “book” post, these blocks will be pre-populated.

Important Notes:

  • Clear Cache: After making changes to your functions.php file, clear your website’s cache (if you’re using a caching plugin) to ensure the changes take effect.
  • Theme Compatibility: Ensure that your theme is compatible with the block editor. Most modern themes are, but older themes might have issues.
  • Plugin Conflicts: If you encounter problems, try disabling other plugins to rule out conflicts.
  • Custom Fields and the Block Editor: When working with custom fields, you might need to use plugins like Advanced Custom Fields (ACF) to integrate them seamlessly with the block editor.1 ACF provides block types that allow you to display your custom field data within the block editor interface.2

By making these changes, you’ll successfully enable the block editor for your “book” custom post type, giving you the flexibility and power of Gutenberg for creating your book content.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *