WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 weeks ago

#18375 new enhancement

Post type templates

Reported by: johnbillion Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: dev-feedback has-patch
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 3 years ago.

Download all attachments as: .zip

Change History (18)

comment:1 johnbillion3 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.

comment:2 johnbillion3 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.

johnbillion3 years ago

comment:3 johnbillion3 years ago

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

comment:4 follow-up: scribu3 years ago

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

comment:5 follow-up: andrewryno3 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.

comment:6 follow-up: scribu3 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 3 years ago by scribu (previous) (diff)

comment:7 in reply to: ↑ 4 johnbillion3 years ago

Replying to scribu:

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

Yep, I agree.

comment:8 in reply to: ↑ 6 johnbillion3 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.

comment:9 in reply to: ↑ 5 johnbillion3 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.

comment:10 sc0ttkclark14 months 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.

comment:11 amtiskaw14 months 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.

comment:12 SergeyBiryukov11 months ago

#24400 was marked as a duplicate.

comment:13 nacin2 months ago

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

comment:14 chrisvanpatten6 weeks 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 :)

comment:15 SergeyBiryukov3 weeks ago

#27500 was marked as a duplicate.

comment:17 lgladdy3 weeks 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 weeks ago by lgladdy (previous) (diff)
Note: See TracTickets for help on using tickets.