WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.34k stars 4.13k forks source link

Allow adding templates for a single post with a specific taxonomy/term in the Site Editor. #48264

Open nefeline opened 1 year ago

nefeline commented 1 year ago

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}:

Screenshot 2023-02-20 at 22 52 48

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.

annezazu commented 1 year ago

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).

jameskoster commented 1 year ago

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.

ntsekouras commented 1 year ago

@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.

annezazu commented 1 year ago

Got it! I'm going to close this out in that case. Happy to re-open if needed.

nefeline commented 1 year ago

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?

jameskoster commented 1 year ago

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:

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.

crimann commented 1 year ago

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:

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.

nefeline commented 1 year ago

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.

jameskoster commented 1 year ago

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?

nefeline commented 1 year ago

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.

audunmb commented 7 months ago

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.