WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 6 weeks ago

#18375 new enhancement

Post type templates

Reported by: johnbillion Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: needs-refresh
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 (1)

18375.patch (8.4 KB) - added by johnbillion 5 years ago.

Download all attachments as: .zip

Change History (25)

#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
3 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
3 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
2 years ago

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

#14 @chrisvanpatten
2 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
2 years ago

#27500 was marked as a duplicate.

#17 @lgladdy
2 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 2 years ago by lgladdy (previous) (diff)

#18 @stewarty
13 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
12 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
2 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 weeks 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 weeks ago

#23 @ericlewis
6 weeks 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 weeks ago

  • Keywords needs-refresh added; has-patch removed
  • Milestone changed from Awaiting Review to Future Release
Note: See TracTickets for help on using tickets.