{"id":3019,"date":"2024-10-24T07:30:50","date_gmt":"2024-10-24T05:30:50","guid":{"rendered":"https:\/\/wypo.io\/?p=3019"},"modified":"2024-10-24T07:31:17","modified_gmt":"2024-10-24T05:31:17","slug":"creating-layout-template-gutenberg","status":"publish","type":"post","link":"https:\/\/wypo.io\/en\/creating-layout-template-gutenberg\/","title":{"rendered":"Creating a layout template with Gutenberg"},"content":{"rendered":"\n<p class=\" hf_animated fade_bottom default  \">Gutenberg offers an intuitive drag-and-drop content creation interface, enabling real-time customization. By defining block templates for your CPT, you guide users in structuring their content, reducing errors and ensuring consistent formatting.<\/p>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop  hf_animated fade_bottom default is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">\n<h2 class=\"wp-block-heading   \" datalink=\"content-defining-the-template-in-a-custom-post-type\">Defining the template in a custom post type<\/h2>\n\n\n\n<p class=\"\">You can also define a predefined block template that the user will see when creating new content for this CPT. This is done with the <code>template<\/code> argument when you register the CPT.<\/p>\n\n\n\n<p class=\"\">Here&#8217;s an example where you define a block template for the CPT <code>portfolio<\/code>:<\/p>\n\n\n    <div class=\"wp-block-habefast-code  \">\n      <header class=\"habefast-code-header\">\n        <div class=\"habefast-code-lang is-lang-php\">\n          PHP        <\/div>\n      <\/header>\n      <textarea\n        class=\"habefast-code-source\"\n        name=\"codemirror-1082878253\"\n        id=\"codemirror-1082878253\"\n      >function create_portfolio_cpt() {\n    $labels = array(\n        &#039;name&#039; =&gt; &#039;Portfolios&#039;,\n        &#039;singular_name&#039; =&gt; &#039;Portfolio&#039;,\n    );\n\n    $args = array(\n        &#039;label&#039; =&gt; &#039;Portfolios&#039;,\n        &#039;public&#039; =&gt; true,\n        &#039;show_in_rest&#039; =&gt; true,\n        &#039;supports&#039; =&gt; array(&#039;title&#039;, &#039;editor&#039;, &#039;thumbnail&#039;, &#039;revisions&#039;, &#039;custom-fields&#039;),\n        &#039;has_archive&#039; =&gt; true,\n        &#039;rewrite&#039; =&gt; array(&#039;slug&#039; =&gt; &#039;portfolio&#039;),\n        &#039;template&#039; =&gt; array(  \/\/ Defining block templates\n            array(&#039;core\/heading&#039;, array(\n                &#039;level&#039; =&gt; 2,\n                &#039;placeholder&#039; =&gt; &#039;Add a heading&#039;,\n            )),\n            array(&#039;core\/paragraph&#039;, array(\n                &#039;placeholder&#039; =&gt; &#039;Add a description&#039;,\n            )),\n            array(&#039;core\/image&#039;, array(\n                &#039;align&#039; =&gt; &#039;center&#039;,\n            )),\n        ),\n        &#039;template_lock&#039; =&gt; &#039;all&#039;,  \/\/ Prevents basic blocks from being deleted\n    );\n\n    register_post_type(&#039;portfolio&#039;, $args);\n}\nadd_action(&#039;init&#039;, &#039;create_portfolio_cpt&#039;);\n<\/textarea>\n      <script>\n        CodeMirror.fromTextArea( document.getElementById('codemirror-1082878253'), {\n          mode: {name:'php',startOpen:true},\n          readOnly: true,\n          lineNumbers: true,\n          firstLineNumber: 1,\n          matchBrackets: true,\n          indentUnit: 4,\n          tabSize: 4,\n          lineWrapping: true,\n        } );\n      <\/script>\n    <\/div>\n    \n\n\n<p class=\"\"><strong>Explanation of options :<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list  \">\n<li class=\"\"><code>template<\/code>: An array containing the default blocks that will be inserted when the user creates new content for this CPT.<\/li>\n\n\n\n<li class=\"\"><code>template_lock<\/code>: Can be set to <code>'all'<\/code> or <code>'insert'<\/code> :\n<ul class=\"wp-block-list  \">\n<li class=\"\"><code>'all'<\/code>: The user can neither modify nor delete defined blocks.<\/li>\n\n\n\n<li class=\"\"><code>'insert'<\/code>: The user can modify the content of blocks, but not delete them or add new ones.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop  hf_animated fade_bottom default is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">\n<h2 class=\"wp-block-heading   \" datalink=\"content-create-a-template-on-an-existing-cpt\">Create a template on an existing CPT<\/h2>\n\n\n\n<p class=\"\">You can use WordPress filters to add block templates to an existing CPT. To do this, you can use the <code>register_post_type_args<\/code> filter to add a block template when the CPT is registered.<\/p>\n\n\n\n<p class=\"\">Example of code to add to your <code>functions.php<\/code> file:<\/p>\n\n\n    <div class=\"wp-block-habefast-code  \">\n      <header class=\"habefast-code-header\">\n        <div class=\"habefast-code-lang is-lang-php\">\n          PHP        <\/div>\n      <\/header>\n      <textarea\n        class=\"habefast-code-source\"\n        name=\"codemirror-973873624\"\n        id=\"codemirror-973873624\"\n      >function add_custom_template_to_cpt($args, $post_type) {\n    \/\/ Replace &#039;portfolio&#039; with your CPT slug\n    if ($post_type === &#039;portfolio&#039;) {\n        $args&#91;&#039;template&#039;&#93; = array(\n            array(&#039;core\/heading&#039;, array(&#039;level&#039; =&gt; 2, &#039;placeholder&#039; =&gt; &#039;Titre&#039;)),\n            array(&#039;core\/paragraph&#039;, array(&#039;placeholder&#039; =&gt; &#039;Description&#039;)),\n            array(&#039;core\/image&#039;, array(&#039;align&#039; =&gt; &#039;center&#039;)),\n        );\n        $args&#91;&#039;template_lock&#039;&#93; = &#039;all&#039;; \/\/ Prevents users from modifying or deleting default blocks\n    }\n    return $args;\n}\nadd_filter(&#039;register_post_type_args&#039;, &#039;add_custom_template_to_cpt&#039;, 10, 2);<\/textarea>\n      <script>\n        CodeMirror.fromTextArea( document.getElementById('codemirror-973873624'), {\n          mode: {name:'php',startOpen:true},\n          readOnly: true,\n          lineNumbers: true,\n          firstLineNumber: 1,\n          matchBrackets: true,\n          indentUnit: 4,\n          tabSize: 4,\n          lineWrapping: true,\n        } );\n      <\/script>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Gutenberg offers an intuitive drag-and-drop content creation interface, enabling real-time customization. By defining block templates for your CPT, you guide users in structuring their content, reducing errors and ensuring consistent formatting. Defining the template in a custom post type You can also define a predefined block template that the user will see when creating new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_habefastfse_class":"","hf_wpseo_meta_title":"How to create a CPT template with Gutenberg [separator] [sitetitle]","hf_wpseo_meta_description":"Discover how to create a template for a personalized content type (CPT) in Gutenberg, and improve the user experience.","hf_wpseo_meta_robots_index":"","hf_wpseo_meta_robots_follow":true,"hf_wpseo_meta_robots_advanced":"{}","hf_wpseo_meta_canonical_url":"","footnotes":""},"categories":[36],"tags":[],"class_list":["post-3019","post","type-post","status-publish","format-standard","hentry","category-gutenberg"],"_links":{"self":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/3019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/comments?post=3019"}],"version-history":[{"count":3,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/3019\/revisions"}],"predecessor-version":[{"id":3022,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/3019\/revisions\/3022"}],"wp:attachment":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/media?parent=3019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/categories?post=3019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/tags?post=3019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}