WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 7 days ago

#18375 new enhancement

Post type templates

Reported by: johnbillion Owned by:
Milestone: 4.7 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch has-unit-tests
Focuses: template Cc:

Description

I'm increasingly finding situations where it would be nice to have the page template functionality available to custom post types (or, more specifically, available to all post types instead of just pages).

From a technical point of view there aren't many changes needed in core to support templates for all post types, but there may be some things to consider regarding the best way to go about it.

Patch coming up for my first pass at it.

Related:

Note that the Custom Post Template plugin has 37k downloads so the demand is there.

Attachments (3)

18375.patch (8.4 KB) - added by johnbillion 5 years ago.
18375.2.patch (11.5 KB) - added by Mte90 5 weeks ago.
new refreshed working version
18375.diff (24.7 KB) - added by swissspidy 9 days ago.

Download all attachments as: .zip

Change History (33)

#1 @johnbillion
5 years ago

  • Keywords has-patch added

Patch that adds per-post-type template functionality to all post types.

A template in a theme can now include an optional 'Template Type' header that specifies the post type that the template is for. If the header isn't present, the template is treated as a page template.

Example:

/*
 * Template Name: Amazing Event
 * Template Type: event
 */

This header placed in a template file will make the Amazing Event template available to the event post type, meaning it can be selected in the Template dropdown on the post editing screen.

#2 @johnbillion
5 years ago

Whoops, messed the first patch up. Updated.

A template can also specify multiple post types it can be used for:

/*
 * Template Name: Amazing Template
 * Template Type: post, page, event
 */

The 'Amazing Template' would be available to select for all posts, pages and events.

@johnbillion
5 years ago

#3 @johnbillion
5 years ago

Third time lucky. Serves me right for running multiple patches on the same install.

#4 follow-up: @scribu
5 years ago

I'm not sure this is worth doing without #11216

#5 follow-up: @andrewryno
5 years ago

Other than having a UI to do it, what's the benefit above just using archive-{post_type}.php, single-{post_type}.php, etc. If you do this for post types, would it only apply that template for the single post page?

I could see it being nice for individual posts or something, but if you are just going to apply it across all the posts within that type, what's the point? I'm probably just missing a proper use case for it, though.

#6 follow-up: @scribu
5 years ago

Yes, these templates would only be used for individual posts.

However, I'm also having difficulty figuring out a use-case for it.

Last edited 5 years ago by scribu (previous) (diff)

#7 in reply to: ↑ 4 @johnbillion
5 years ago

Replying to scribu:

I'm not sure this is worth doing without #11216

Yep, I agree.

#8 in reply to: ↑ 6 @johnbillion
5 years ago

Replying to scribu:

Yes, these templates would only be used for individual posts.

However, I'm also having difficulty figuring out a use-case for it.

The use-case is exactly the same as the use-case for page templates. It allows users to select different templates on a per-post basis.

#9 in reply to: ↑ 5 @johnbillion
5 years ago

Replying to andrewryno:

I could see it being nice for individual posts or something, but if you are just going to apply it across all the posts within that type, what's the point? I'm probably just missing a proper use case for it, though.

I think you're misunderstanding slightly. All this is doing is applying the page templates functionality (ie. the dropdown menu that allows users to select from several available custom page templates) to all post types instead of just pages.

#10 @sc0ttkclark
4 years ago

  • Cc lol@… added

What about doing this as a 'supports' option for post types? 'supports' => array( 'title', 'editor', 'page-attributes', 'page-template' )

One caveat would be that it would require page-attributes to be shown.

Never-the-less, I'd like to be able to use Page Templates for Custom Post Types, I'm not sure post-type specific page templates would be better.

#11 @amtiskaw
4 years ago

  • Cc amtiskaw added

I have a requirement on my current project to let users choose between a regular and full-width template for a custom post type. No problem, I thought, But then I discovered template selection is only possible for regular posts, and not for custom post types.

I figured there must be some difficult obstacle to implementing this, but now I see that someone has already implemented it, over a year and half ago, but nobody has bothered to land the patch? What exactly is the hold-up here? All I see in this thread is quibbling by people who don't even seem to understand what page templates are for.

#12 @SergeyBiryukov
3 years ago

#24400 was marked as a duplicate.

#13 @nacin
3 years ago

  • Component changed from Themes to Posts, Post Types
  • Focuses template added

#14 @chrisvanpatten
3 years ago

Just to offer potential use-cases, I'm working on a project that needs the ability to make "longreads" posts look different (sort of in the "art-directed" style of The Verge/Polygon/et al), and we're creating a few different templates so the editors can choose different styles for each of those articles. Post/Page Templates are perfect for that.

I also have another client looking to have certain posts show author boxes and avatars, among other small changes (these are posts from "guest contributors" vs. posts from regular writers). There are plenty of ways to handle that, but Post Templates seem like a natural fit that's easy for the client to use.

I'm using the Post Template plugin that @johnbillion linked to above, but having it in core would be even better :)

#15 @SergeyBiryukov
3 years ago

#27500 was marked as a duplicate.

#17 @lgladdy
3 years ago

We're building a massive site for a major public sector organisation who want to be able to create pages, but in order to separate the core "corporate" pages with other pages, we wanted to create CPTs to handle that segmentation, but they need to support templating for us to do that.

Last edited 3 years ago by lgladdy (previous) (diff)

#18 @stewarty
18 months ago

Just to add a new use case here.

Currently building a project involving landing pages.

Landing pages are setup as a custom post type but we need multiple templates. (Three different templates)

We've engineered around the problem now, the odd thing is that it seems WordPress goes out of it way to stop you implementing this at every stage. Full of post_type == 'page' conditionals that you can't really avoid so you end up recreating a lot of core code in a plugin.

Would be nice to see this officially supported.

#19 @jhned
17 months ago

Just to add another use case-- we recently wanted to add a new format to our custom post type "Projects" for case studies. Since the case studies format has completely different markup, content and styles, I needed to set it up as a page template, and used a plugin that extended the functionality to the custom post type.

#20 @Mckilem
7 months ago

Given the increased demands by clients for post templates (we do have some options, such as few outdated plugins and plugins such as display widgets to control which widget shows on which post), but all of that would not be necessary if there would be an option to use templates not only on pages, but on posts and all other custom post types.

What are the current considerations on whether we can expect this in future releases?

#21 @mor10
6 months ago

For years I've been asking
why Pages were unique,
why Custom and Template are words reserved
for content belonging to the singular, static clique.

What is a Page but an island?
Unconnected, alone, a windswept highland
bereft the bridges that tie it to others
its only relations the family it fosters.

Is this why Template Name is reserved for Pages?
A feeble gesture to make them feel they don't live in cages?

Or is it something deeper?
A notion that Posts must be kept in line,
that uniqueness in ephemera
is the aftermath of leaded wine.

What world would we live in
if Posts showed their true nature?
How wild and chaotic
how unfitting our beloved nomenclature.

"Cast freedom to the wayside" they say:
"If Posts want character,
let them try:
Fit the Post Formats preordained,
conform,
or scrape by," on a wing and a prayer
that a non-standard solution
is better than codified absolution.

To this I have but one thing to say:
Posts are unique, so let them have their way.
Templates are what we build,
for posts,
for pages,
it's the nature of our Guild.

Let's end this segregation,
this illusion that only Pages are unique.
All post types deserve templates!
It's such a small, harmless tweak.

This ticket was mentioned in Slack in #core by sc0ttkclark. View the logs.


6 months ago

#23 @ericlewis
6 months ago

  • Keywords dev-feedback removed

A bunch of people have provided use cases for this, and this seems like a sensical enhancement. Adding to future release.

The patch is rather old, will need a refresh as well as account for the last five years.

#24 @ericlewis
6 months ago

  • Keywords needs-refresh added; has-patch removed
  • Milestone changed from Awaiting Review to Future Release

#25 @jorbin
5 weeks ago

  • Keywords needs-unit-tests added

This would be nice to get into 4.7. To do so, it's going to require a refresh of the patch and some unit tests.

@Mte90
5 weeks ago

new refreshed working version

#26 @Mte90
5 weeks ago

  • Keywords has-patch dev-feedback added; needs-refresh removed

That patch contains respect the old one:

  • The check to include the Attributs metabox in admin it's moved inside the metabox to show the Template in all the post types and Order and Parent are showed only on Page post type
  • The page_templates key for cache changed on post_templates
  • Added theme_page_templates filter

Respect to the old:

  • Updated the code in few part of the code itself
  • Use the new WP internal for get the header information about the page name

#27 @swissspidy
10 days ago

  • Keywords dev-feedback removed

Thanks for the patch @Mte90! I'll look into it and try to provide some tests.

@swissspidy
9 days ago

#28 @swissspidy
9 days ago

  • Keywords has-unit-tests added; needs-unit-tests removed
  • Milestone changed from Future Release to 4.7

In 18375.diff:

  • Do not introduce new functions everywhere, at least for now.
  • Remove checks for the 'post' post type in various places (get_page_template_slug(), WP_Posts_List_Table (quick edit), is_page_template(), wp_insert_post() etc.)
  • Also makes this work in the WP_Post class
  • Adds a new attributes post type label as it now affects much more post types
  • Adds unit tests or modifies existing ones where necessary.

Basically, this adds support for custom templates for any post type that supports page-attributes. That means with the current patch it doesn't work for posts by default.

To do:

  • Discuss backward compatibility:
    • Should the 'page_templates' cache group change to 'post_templates' or should there be both? If it should change, the docs inside WP_Theme need to reflect that.
    • Add new params or new is_post_type_template() / get_post_type_templates() functions? It's totally confusing when there's get_page_templates() and get_page_template()
    • Should the 'post' post type have the page-attributes support by default?
  • Test all the things: custom post types, posts, pages, quick edit, etc.

Feedback welcome.

#29 @Mte90
9 days ago

Well the new code is amazing :-D

Should the 'page_templates' cache group change to 'post_templates' or should there be both? If it should change, the docs inside WP_Theme need to reflect that.

As I can see in the code the only reference for that value is inside WP_Theme so maybe it's better to check if there are plugins that use that value.

Add new params or new is_post_type_template() / get_post_type_templates() functions? It's totally confusing when there's get_page_templates() and get_page_template()

I think to maintain the old for compatibility and new one to use the new functions and to avoid confusions.

Should the 'post' post type have the page-attributes support by default?

I don't think probably there are not many website that will use this feature so enable mandatory is not the best way. And also only the people that know that feature will use it.

#30 @swissspidy
7 days ago

I'll try to update the patch over the next day.

I think we should use get_file_data() for retrieving the headers as it's more reliable and reduces the risk of getting false positives. The current regex is over the whole PHP file.

Note: See TracTickets for help on using tickets.