{"id":1169,"date":"2022-02-23T12:43:11","date_gmt":"2022-02-23T17:43:11","guid":{"rendered":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/?p=1169"},"modified":"2022-02-23T12:53:09","modified_gmt":"2022-02-23T17:53:09","slug":"adding-a-block-template-to-a-cpt","status":"publish","type":"post","link":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/2022\/adding-a-block-template-to-a-cpt\/","title":{"rendered":"Adding a Block Template to a CPT"},"content":{"rendered":"\n<p>If you have a WordPress website with an existing custom post type and you want to add a Gutenberg block template to be used for every new post of this custom post type there are two ways of doing this.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>If you can access the code that registers the post type, you can just add <code>template<\/code> as an argument in that code. You can see more about how to do that in the <a rel=\"noreferrer noopener\" href=\"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/block-api\/block-templates\/#custom-post-types\" target=\"_blank\">WordPress developer documentation <\/a>for example. <\/p>\n\n\n\n<p>If you can&#8217;t access the code that registers the post type you can still add arguments to it, including an argument to add a block template to new posts of that custom post type, with the <code>register_post_type_args<\/code> filter. See here below:<\/p>\n\n\n\n<pre title=\"Changing an Existing CPT\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php line-numbers\">add_filter( 'register_post_type_args', 'bw_change_cpt', 10, 2 );\nfunction bw_change_cpt( $args, $post_type ){\n \n    if( $post_type == 'portfolio' ){\n        $args['template'] = [['core\/post-featured-image']];\n    }\n \n    return $args;\n}<\/code><\/pre>\n\n\n\n<p>Now, if you were to click on <strong>Add New <\/strong>under <strong>Portfolio<\/strong> on this WordPress website, you would see a new post with a block for a featured image already in place.<\/p>\n\n\n\n<p>I&#8217;ve named the function <code>bw_change_cpt<\/code> but you can name it whatever you want of course, just make sure you are consistent with it, it&#8217;s referenced in two places, so you&#8217;ll need to change both. And check to make sure you have the correct name for the custom post type. The one in this example is called <code>portfolio<\/code>, if yours is called differently be sure to change that.<\/p>\n\n\n\n<p>For an example of how to add more or different blocks, or how to set parameters for a block, you can also look at the <a rel=\"noreferrer noopener\" href=\"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/block-api\/block-templates\/#custom-post-types\" target=\"_blank\">WordPress developer documentation<\/a>, but this <a rel=\"noreferrer noopener\" href=\"https:\/\/www.elegantthemes.com\/blog\/wordpress\/how-to-create-gutenberg-templates\" target=\"_blank\">elegant themes article <\/a>also has a few good tips under Method 3 and <a href=\"https:\/\/awhitepixel.com\/blog\/wordpress-gutenberg-control-blocks-remove-and-block-templates\/\" target=\"_blank\" rel=\"noreferrer noopener\">A White Pixel<\/a> has some good examples too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have a WordPress website with an existing custom post type and you want to add a Gutenberg block template to be used for every new post of this custom post type there are two ways of doing this.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[220,217,216,219,218,41],"class_list":["post-1169","post","type-post","status-publish","format-standard","hentry","category-new-things","tag-a-white-pixel","tag-block-editor","tag-custom-post-type","tag-elegant-themes","tag-gutenberg","tag-wordpress","infinite-scroll-item"],"_links":{"self":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/1169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/comments?post=1169"}],"version-history":[{"count":2,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/1169\/revisions"}],"predecessor-version":[{"id":1178,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/1169\/revisions\/1178"}],"wp:attachment":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/media?parent=1169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/categories?post=1169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/tags?post=1169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}