Repository navigation
How to dynamically load template parts in block themes #459
Replies: 3 comments
|
This is essentially the equivalent of doing this in classic themes: get_sidebar($var)
get_header($var)
get_footer($var)
get_template_part($slug, $var) |
0 replies
|
The Snippet was approved. The conversation continues on the issue |
0 replies
|
Published here: https://developer.wordpress.org/news/2026/06/dynamically-loading-template-parts-in-block-themes/ |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Here's a quick snippet I used to dynamically load a category-based sidebar template for single posts:
Essentially, it looks for
parts/sidebar-post-{category}.htmland falls back toparts/sidebar-post.html, allowing theme authors to create sidebars based on the post category.There's loads of things that you can do with this, though. It's not limited to sidebars and could be realistically used to replace any template part based on the given data.
This is just an easy snippet, but I could see it being wrapped up into a longer tutorial.
All reactions