{"id":96,"date":"2025-03-01T05:07:35","date_gmt":"2025-03-01T05:07:35","guid":{"rendered":"https:\/\/notes.zj1963.com\/?p=96"},"modified":"2025-03-01T05:09:26","modified_gmt":"2025-03-01T05:09:26","slug":"new-post-type-template-using-blockeditor","status":"publish","type":"post","link":"https:\/\/notes.zj1963.com\/?p=96","title":{"rendered":"New Post Type template using blockeditor"},"content":{"rendered":"\n<p><a href=\"https:\/\/notes.zj1963.com\/?p=94\">\u5148\u8bfb\u8fd9\u4e2a Post Type \u2013 \u7b14\u8bb0<\/a><\/p>\n\n\n\n<p>To enable the block editor (Gutenberg) for your custom post type &#8220;book&#8221; (or whatever your post type is), you need to modify the <code>register_post_type<\/code> arguments within your <code>functions.php<\/code> file or custom plugin. Specifically, you need to add or modify the <code>supports<\/code> argument.<\/p>\n\n\n\n<p>Here&#8217;s how you can do it:<\/p>\n\n\n\n<p><strong>1. Modify the <code>supports<\/code> Argument:<\/strong><\/p>\n\n\n\n<p>In your <code>create_book_post_type()<\/code> function (or the function where you register your custom post type), locate the <code>supports<\/code> array.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If <code>supports<\/code> doesn&#8217;t exist, create it.<\/li>\n\n\n\n<li>If <code>supports<\/code> exists, add or ensure that <code>'editor'<\/code> is included.<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s the modified code snippet:<\/p>\n\n\n\n<p>PHP<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function create_book_post_type() {\n    \/\/ ... (Your labels and other args) ...\n\n    $args = array(\n        \/\/ ... (Your other arguments) ...\n        'supports'            =&gt; array( 'title', 'editor', 'thumbnail', 'custom-fields' ), \/\/ Ensure 'editor' is present\n        \/\/ ... (Your other arguments) ...\n    );\n    register_post_type( 'book', $args );\n}\nadd_action( 'init', 'create_book_post_type', 0 );\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>'editor'<\/code> element within the <code>supports<\/code> array tells WordPress to enable the block editor for your custom post type.<\/li>\n\n\n\n<li>By default, if the <code>'editor'<\/code> support is not declared, then WordPress will default to the classic editor.<\/li>\n<\/ul>\n\n\n\n<p><strong>2. Enable Template Support (Optional but Recommended):<\/strong><\/p>\n\n\n\n<p>If you want to use block templates (predefined block layouts) for your &#8220;book&#8221; post type, you can also add <code>'template'<\/code> to the <code>supports<\/code> array:<\/p>\n\n\n\n<p>PHP<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$args = array(\n    \/\/ ...\n    'supports' =&gt; array( 'title', 'editor', 'thumbnail', 'custom-fields', 'template' ),\n    \/\/ ...\n);\n<\/code><\/pre>\n\n\n\n<p><strong>3. Create Block Templates (Optional):<\/strong><\/p>\n\n\n\n<p>If you enabled template support, you can create block templates to define the initial content and layout for new &#8220;book&#8221; posts. Here&#8217;s how:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Within your theme&#8217;s <code>functions.php<\/code> file or a custom plugin, use the <code>register_block_template<\/code> function.<\/li>\n<\/ul>\n\n\n\n<p>PHP<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function register_book_template() {\n    $post_type_object = get_post_type_object( 'book' );\n    $post_type_object-&gt;template = array(\n        array( 'core\/paragraph', array(\n            'placeholder' =&gt; 'Enter book description here...',\n        ) ),\n        array( 'core\/image', array(\n            'align' =&gt; 'center',\n        )),\n        array( 'core\/heading', array(\n            'placeholder' =&gt; 'Additional Information',\n            'level' =&gt; 3,\n        )),\n    );\n}\nadd_action( 'init', 'register_book_template' );\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This code defines a template with a paragraph, an image, and a heading. When you create a new &#8220;book&#8221; post, these blocks will be pre-populated.<\/li>\n<\/ul>\n\n\n\n<p><strong>Important Notes:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Clear Cache:<\/strong> After making changes to your <code>functions.php<\/code> file, clear your website&#8217;s cache (if you&#8217;re using a caching plugin) to ensure the changes take effect.<\/li>\n\n\n\n<li><strong>Theme Compatibility:<\/strong> Ensure that your theme is compatible with the block editor. Most modern themes are, but older themes might have issues.<\/li>\n\n\n\n<li><strong>Plugin Conflicts:<\/strong> If you encounter problems, try disabling other plugins to rule out conflicts.<\/li>\n\n\n\n<li><strong>Custom Fields and the Block Editor:<\/strong> When working with custom fields, you might need to use plugins like Advanced Custom Fields (ACF) to integrate them seamlessly with the block editor.<sup>1<\/sup> ACF provides block types that allow you to display your custom field data within the block editor interface.<sup>2<\/sup><\/li>\n<\/ul>\n\n\n\n<p>By making these changes, you&#8217;ll successfully enable the block editor for your &#8220;book&#8221; custom post type, giving you the flexibility and power of Gutenberg for creating your book content.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u8bfb\u8fd9\u4e2a Post Type \u2013 \u7b14\u8bb0 To enable the block editor (Gutenberg) for your custom post type &#8220;book&#8221; (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&#8217;s how you can do it: 1. Modify [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=96"}],"version-history":[{"count":3,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/96\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}