This follows the dynamically loading template parts in block themes approach from the WordPress Developer Blog.
What It Does
Templates keep a single core/template-part reference (for example header, footer, or sidebar). Before the block renders, Aegis lets you change the slug through a filter. If the alternate part file exists, WordPress loads it; otherwise the original part is kept.
Out of the box nothing changes visually. Child themes and custom plugins opt in via aegis_dynamic_template_part_slug.
How It Works
Copied!JSONflowchart TD renderBlock["core/template-part about to render"] --> filterHook["aegis_dynamic_template_part_slug filter"] filterHook -->|slug unchanged| defaultPart[Load original part] filterHook -->|new slug returned| fileCheck{"parts/{slug}.html exists?"} fileCheck -->|yes| altPart[Load alternate part] fileCheck -->|no| defaultPart
-
render_block_dataintercepts every block before render. - For
core/template-parton the front end, Aegis appliesaegis_dynamic_template_part_slug. - If the filter returns a different slug and
parts/{slug}.htmlexists (theme or child theme), that slug is used. - Skip-link injection and
aegis_before_*/aegis_after_*hooks fire on whichever part actually renders.
Implemented by Aegis\Framework\DesignSystem\DynamicTemplateParts.
Filter Reference
| Filter | aegis_dynamic_template_part_slug |
|---|---|
| Parameters |
$slug (string), $parsed_block (array) |
| Return | Template part slug string |
| Default | The slug from the block attributes (unchanged) |
Return the original $slug (or a non-string / empty value) to leave the part unchanged. Only filesystem parts under the theme’s template-part folder (usually parts/) are accepted.
Examples
Add these through a plugin: a Functions (PHP) snippet in Aegis → Code Snippets or a site-specific plugin. See Where to Put Snippets. Create the matching parts/*.html files first.
Category-based sidebar on single posts
Assumes the template references sidebar (for example page-with-sidebar or a custom single layout). Create parts/sidebar-news.html, parts/sidebar-reviews.html, and so on as needed.
Copied!PHPadd_filter( 'aegis_dynamic_template_part_slug', static function ( string $slug, array $parsed_block ): string { if ( 'sidebar' !== $slug || ! is_singular( 'post' ) ) { return $slug; } $post = get_queried_object(); if ( ! $post instanceof WP_Post ) { return $slug; } $parts_dir = get_block_theme_folders()['wp_template_part']; foreach ( get_the_category( $post->ID ) as $category ) { $candidate = 'sidebar-' . $category->slug; if ( locate_template( "{$parts_dir}/{$candidate}.html" ) ) { return $candidate; } } return $slug; }, 10, 2);
Minimal header on landing pages
Create parts/header-landing.html, then swap when the Blank or landing page template is in use:
Copied!PHPadd_filter( 'aegis_dynamic_template_part_slug', static function ( string $slug ): string { if ( 'header' !== $slug ) { return $slug; } if ( is_page_template( 'blank' ) || is_page_template( 'page-landing' ) ) { return 'header-landing'; } return $slug; });
Simplified checkout footer
Create parts/footer-checkout.html:
Copied!PHPadd_filter( 'aegis_dynamic_template_part_slug', static function ( string $slug ): string { if ( 'footer' !== $slug ) { return $slug; } if ( function_exists( 'is_checkout' ) && is_checkout() && ! is_wc_endpoint_url() ) { return 'footer-checkout'; } return $slug; });
Creating Alternate Parts
Prefer filesystem parts in the theme or child theme:
Copied!Directory treeparts/header-landing.htmlparts/footer-checkout.htmlparts/sidebar-news.html
You can use a thin pattern wrapper (same as the shipped header, footer, and sidebar parts) or full block markup.
Site Editor–created template parts are unreliable for custom slugs today (Gutenberg issue 57629). Control slugs via theme files whenever possible.
Related
- Template Parts — Header, footer, and sidebar parts
-
Hooks and Filters — Filter reference including
aegis_dynamic_template_part_slug - Hook Patterns (Theme Injection Hooks) — Injection hooks around template parts
- Dynamically loading template parts in block themes — Developer Blog tutorial