Allow adding templates for a single post with a specific taxonomy/term in the Site Editor.
What problem does this address?
Currently, it is not possible to add templates for a single post with a specific taxonomy/term in the Site Editor.
What is your proposed solution?
This is a request for making this option available under Templates > Single Item: {post_type}:
On WooCommerce Blocks, we have been exploring different approaches for allowing store owners to add custom templates to individual products depending on their type (simple, grouped, variable, external, etc.). In a nutshell, these are terms of the product_type taxonomy. Ideally, we would like to rely on a more robust long-term solution provided by Gutenberg to achieve this goal: we believe this approach can benefit not only Woo, but basically anyone who intends to have a special template for a single post type given its taxonomy.
Related discussion around scaling the template interface https://github.com/WordPress/gutenberg/issues/42325 cc @jameskoster for your thoughts here (especially with your Woo background).
I think the issue here is that the wp template hierarchy does not cater to this use case, and so far as I understand things there's no way to add the necessary logic to something like the single-product template. Perhaps @ntsekouras can offer some input on the technical details.
@jameskoster is right about the template hierarchy and the handling of templates.
This seems to me more of a specific term/taxonomy template, which is already handled from the UI.
Got it! I'm going to close this out in that case. Happy to re-open if needed.
Hi there, folks!
Thanks for your responses, I'd like to offer a new point of view here:
I think the issue here is that the wp template hierarchy does not cater to this use case, and so far as I understand things there's no way to add the necessary logic to something like the single-product template. Perhaps @ntsekouras can offer some input on the technical details.
Yes, it is true the native template hierarchy for a single post currently doesn't support this (we also noted this during our exploration), but since on the PHP side, we can rely on a hook (e.g. 'single_template_hierarchy') to extend the native hierarchy, it would be important to be able to visualize those on this modal, if not programmatically (have the new template automatically displayed on this modal), at least provide a way for plugin developers to extend it so we can add/remove templates and customize this area, ensuring it is aligned with the extended template hierarchy. What do you think?
This seems to me more of a specific term/taxonomy template, which is already handled from the UI.
I'm not sure that's quite right. I think the use case here is about displaying single entries with a specific term using a unique template, e.g. "I want everything published in the Recipes category to appear this way".
Technically you can already do this by creating a $custom template and applying it manually to all those posts, but that is quite tedious and would have to be done each time you create a new entry. It may still be a short-term solution for you @nefeline ?
There does seem to be a valid use case here, but I'm curious about the hierarchy structure. What would happen if templates existed for different terms in the same taxonomy, but a post was assigned to both terms? IE:
- All posts in the Recipes category are assigned a template
- All posts in the Reviews category are assigned a template
- A post was categorised both as a Recipe and Review
Which template gets used there? How can we communicate this to users? How does the add template modal scale to accommodate many taxonomies / terms?
I'll re-open for now. Easy to close again if we deem this to be plugin territory.
The proposed solution by @nefeline would make a lot of sense to me as well. For example, I have a category with "Notes" on my blog, that I want to display differently than other posts. Right now, there's no way (as far as I can tell) rather than:
- making a Custom Template, and then applying it manually on each note (not practical)
- moving Notes to a post type instead of a category (which is something I considered anyway, but shouldn't be necessary)
While it's true that in classic themes we did't have a single-{category}.php template, we did have conditionals! For smaller changes (for example: "If post is of a given category -> don't display a title, or author, or date") we were often using the single.php template with conditionals to check for taxonomies/authors and what-not. As we can't use conditionals in the site editor, this does not seem to be possible at the moment.
Thanks @jameskoster!
Technically you can already do this by creating a $custom template and applying it manually to all those posts, but that is quite tedious and would have to be done each time you create a new entry.
Yes, indeed: While it's possible to manually create custom templates and apply them manually to individual posts, that's suboptimal and doesn't scale: for a website with hundreds/thousands of posts, it can be a challenge to keep up and ensure all individual posts under the same category or term share the same template via manual user input.
There does seem to be a valid use case here, but I'm curious about the hierarchy structure. What would happen if templates existed for different terms in the same taxonomy, but a post was assigned to both terms? IE:
All posts in the Recipes category are assigned a template All posts in the Reviews category are assigned a template A post was categorised both as a Recipe and Review Which template gets used there? How can we communicate this to users? How does the add template modal scale to accommodate many taxonomies / terms?
In this scenario, if a given post has multiple categories/terms (with custom templates associated with them), an option here could be to automatically list them under the template options for the individual post:
https://user-images.githubusercontent.com/15730971/223856085-cddfe4a4-2351-4425-b8ce-a886c3d5711a.mov
Looking forward to hearing your thoughts.
Yeah, I guess that's the problem I see here. The motivation is to establish automatic template selection and avoid the tedium of manually applying $custom templates. But if determining which template to use is going to wind up involving user intervention anyway, doesn't that defeat the purpose a bit?
Yeah, I guess that's the problem I see here. The motivation is to establish automatic template selection and avoid the tedium of manually applying $custom templates. But if determining which template to use is going to wind up involving user intervention anyway, doesn't that defeat the purpose a bit?
I see your point: one way to circumvent this could be to auto-assign the template for whatever category the user added first, minimizing the user manual intervention to only edge cases, where multiple categories/terms have associated templates.
The motivation is to establish automatic template selection and avoid the tedium of manually applying $custom templates.
Avoiding the tedium of manually applying $custom templates is for sure one of the main motivations here, but another one is also to ensure we are not restricting the customization capabilities that WordPress currently supports.
On the PHP side, WordPress already allows extending and modifying the native template hierarchy for a single post via the 'single_template_hierarchy' hook, allowing us to create a template exclusively for posts with a specific category, so it would be essential to support the same via the site editor so site owners and plugin/theme developers can reliably migrate their classic templates to the new FSE experience without requiring manual user intervention.
If not programmatically (have the new template automatically displayed on this modal), at least provide a way for plugin developers to extend it so we can add/remove templates and customize this area, ensuring it is aligned with the extended template hierarchy.
Some of the use cases (like Notes) could probably be better solved with Post Formats as discussed here #53049, but applying some styling by category would still be nice. Different colors in the header, background etc, depending on category would be something I would use at least.
@jameskoster @nefeline @annezazu hi there! Do you have any news about this feature? I am setting up a pretty complex website using the new Twenty Twenty-Five theme and this would help me a lot shape my overall structure.
@PascalClaro there are no further updates here than what you can see above :)
Even something like adding the taxonomies to the body classes by default would be an improvement.
These days, with AI, it's not that hard to write a plugin / hack for your child theme to add this functionality. This worked for me just now and I hope it's a good starting point for anybody else searching for a solution: https://github.com/nextab/nxt-template-routing
Even something like adding the taxonomies to the body classes by default would be an improvement.
If you look into the filter add_body_class(), you can accomplish this relatively easily. (This is assuming your theme will use the WordPress default function to "print" the body classes, but that's true for 90% of all themes.)
I currently have several projects in the works where I would really love to have exactly what’s described here. Interestingly, this works with Elementor and classic themes, but not with Gutenberg components :(