WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/26/2016 08:06:43 AM (3 years ago)
Author:
swissspidy
Message:

Posts, Post Types: Add support for post type templates.

WordPress has supported custom page templates for over 12 years, allowing developers to create various layouts for specific pages.
While this feature is very helpful, it has always been limited to the 'page' post type and not was not available to other post types.

By opening up the page template functionality to all post types, we continue to improve the template hierarchy's flexibility.

In addition to the Template Name file header, the post types supported by a template can be specified using Template Post Type: post, foo, bar.
When at least one template exists for a post type, the 'Post Attributes' meta box will be displayed in the back end, without the need to add post type support for 'page-attributes'. 'Post Attributes' can be customized per post type using the 'attributes' label when registering a post type.

Props johnbillion, Mte90, dipesh.kakadiya, swissspidy.
Fixes #18375.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/template.php

    r38789 r38951  
    450450 * The hierarchy for this template looks like:
    451451 *
    452  * 1. single-{post_type}-{post_name}.php
    453  * 2. single-{post_type}.php
    454  * 3. single.php
    455  *
    456  * An example of this is:
    457  *
    458  * 1. single-post-hello-world.php
    459  * 2. single-post.php
    460  * 3. single.php
     452 * 1. {Post Type Template}.php
     453 * 2. single-{post_type}-{post_name}.php
     454 * 3. single-{post_type}.php
     455 * 4. single.php
     456 *
     457 * An example of this is:
     458 *
     459 * 1. templates/full-width.php
     460 * 2. single-post-hello-world.php
     461 * 3. single-post.php
     462 * 4. single.php
    461463 *
    462464 * The template hierarchy is filterable via the {@see 'single_template_hierarchy'} hook.
     
    467469 * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
    468470 *              template hierarchy when the post name contains multibyte characters.
     471 * @since 4.7.0 {Post Type Template}.php was added to the top of the template hierarchy.
    469472 *
    470473 * @see get_query_template()
     
    478481
    479482    if ( ! empty( $object->post_type ) ) {
     483        $template = get_page_template_slug( $object );
     484        if ( $template && 0 === validate_file( $template ) ) {
     485            $templates[] = $template;
     486        }
    480487
    481488        $name_decoded = urldecode( $object->post_name );
Note: See TracChangeset for help on using the changeset viewer.