Building Block Theme Templates and Template Parts with Full Site Editing

Block theme templates for a custom post type

In a classic theme, a portfolio post type gets single-portfolio.php with its own header and sidebar. A block theme follows the same template hierarchy, but the templates are HTML files with block markup, and the reusable pieces are template parts. Once the files are in the right place, the site owner can also change them in the Site Editor without touching code.

Problem: A portfolio theme is moved from PHP templates to a block theme. The portfolio items need a layout with a sidebar for project details, and some items should use a full-width layout without the sidebar, selectable per item.

Solution: Create templates/single-portfolio.html for the post type, put the sidebar into a template part parts/portfolio-details.html, register an alternative template single-portfolio-wide in the customTemplates section of theme.json with postTypes, and declare the template part in templateParts.

The files

ha-theme/
├── style.css
├── theme.json
├── templates/
│   ├── index.html
│   ├── single.html
│   ├── single-portfolio.html
│   └── single-portfolio-wide.html
└── parts/
    ├── header.html
    ├── footer.html
    └── portfolio-details.html

single-portfolio.html is found by the template hierarchy automatically, exactly like single-portfolio.php: single-{post_type}-{slug}, then single-{post_type}, then single, then index. The post type must be registered with 'public' => true. The basic structure of a block theme: WordPress 5.9 Full Site Editing.

templates/single-portfolio.html

<!-- wp:template-part {"slug":"header","tagName":"header"} /-->

<!-- wp:group {"tagName":"main","layout":{"type":"constrained","wideSize":"1200px"}} -->
<main class="wp-block-group">
    <!-- wp:post-title {"level":1,"align":"wide"} /-->

    <!-- wp:columns {"align":"wide"} -->
    <div class="wp-block-columns alignwide">
        <!-- wp:column {"width":"66.66%"} -->
        <div class="wp-block-column" style="flex-basis:66.66%">
            <!-- wp:post-featured-image {"aspectRatio":"16/9"} /-->
            <!-- wp:post-content {"layout":{"type":"constrained"}} /-->
        </div>
        <!-- /wp:column -->

        <!-- wp:column {"width":"33.33%"} -->
        <div class="wp-block-column" style="flex-basis:33.33%">
            <!-- wp:template-part {"slug":"portfolio-details","tagName":"aside"} /-->
        </div>
        <!-- /wp:column -->
    </div>
    <!-- /wp:columns -->
</main>
<!-- /wp:group -->

<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->

The wp: comments are what WordPress reads; the HTML between them must match what the blocks would save, otherwise the Site Editor shows “This block contains unexpected or invalid content”. The safest way to get correct markup is to build the template in the Site Editor once and copy it with “Export” (Tools → Export in the Site Editor) or the code editor view.

parts/portfolio-details.html

<!-- wp:group {"style":{"spacing":{"padding":{"top":"var:preset|spacing|30","bottom":"var:preset|spacing|30","left":"var:preset|spacing|30","right":"var:preset|spacing|30"}}},"backgroundColor":"base-2","layout":{"type":"flow"}} -->
<div class="wp-block-group has-base-2-background-color has-background" style="padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--30)">
    <!-- wp:heading {"level":2,"fontSize":"medium"} -->
    <h2 class="wp-block-heading has-medium-font-size">Project details</h2>
    <!-- /wp:heading -->

    <!-- wp:post-terms {"term":"portfolio_category"} /-->

    <!-- wp:post-date /-->
</div>
<!-- /wp:group -->

The color base-2 and the spacing preset 30 must exist in the palette and spacing sizes of the theme’s theme.json. core/post-terms works with any taxonomy registered for the post type.

theme.json: the alternative template and the part

{
    "$schema": "https://schemas.wp.org/wp/6.6/theme.json",
    "version": 3,
    "customTemplates": [
        {
            "name": "single-portfolio-wide",
            "title": "Portfolio, full width",
            "postTypes": [ "portfolio" ]
        }
    ],
    "templateParts": [
        { "name": "header", "title": "Header", "area": "header" },
        { "name": "footer", "title": "Footer", "area": "footer" },
        { "name": "portfolio-details", "title": "Portfolio details", "area": "uncategorized" }
    ]
}

customTemplates makes templates/single-portfolio-wide.html selectable in the Template panel of the editor, only for portfolio items because of postTypes. Without postTypes, a custom template is offered for pages only. The chosen template is stored in the post meta _wp_page_template, as with PHP page templates. Version 3 of theme.json came with WordPress 6.6; for themes that support 6.5 and older, use "version": 2, the two sections are the same.

When the Site Editor wins

As soon as someone saves a template or a part in the Site Editor, WordPress stores the edited version in the database (post types wp_template and wp_template_part) and uses it instead of the file. A changed file in the theme then has no effect on that site. “Reset” in the Site Editor deletes the database version and the file is used again. Keep this in mind when a deployment “does not work”. More on template parts and patterns: block patterns and template parts in a theme.

NOTE: The area of a template part (header, footer, uncategorized) controls where the Site Editor lists it and which icon it gets; it does not change the HTML. The HTML element comes from tagName in the template where the part is used.