Opened 22 months ago

Last modified 3 months ago

#18375 new enhancement

Post type templates

Reported by: johnbillion Owned by:
Priority: normal Milestone: Awaiting Review
Component: Themes Version:
Severity: normal Keywords: dev-feedback has-patch
Cc: lol@…, amtiskaw

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 22 months ago.

Download all attachments as: .zip

Change History (12)

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

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.

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

comment:4 follow-up: ↓ 7   scribu22 months ago

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

comment:5 follow-up: ↓ 9   andrewryno22 months 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: ↓ 8   scribu22 months ago

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

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

Version 0, edited 22 months ago by scribu (next)

comment:7 in reply to: ↑ 4   johnbillion22 months ago

Replying to scribu:

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

Yep, I agree.

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

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

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

Note: See TracTickets for help on using tickets.