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-admin/includes/template.php

    r38831 r38951  
    294294    <div class="post_password">' . esc_html( $post->post_password ) . '</div>';
    295295
    296     if ( $post_type_object->hierarchical )
     296    if ( $post_type_object->hierarchical ) {
    297297        echo '<div class="post_parent">' . $post->post_parent . '</div>';
    298 
    299     if ( $post->post_type == 'page' )
    300         echo '<div class="page_template">' . esc_html( get_post_meta( $post->ID, '_wp_page_template', true ) ) . '</div>';
    301 
    302     if ( post_type_supports( $post->post_type, 'page-attributes' ) )
     298    }
     299
     300    echo '<div class="page_template">' . esc_html( $post->page_template ) . '</div>';
     301
     302    if ( post_type_supports( $post->post_type, 'page-attributes' ) ) {
    303303        echo '<div class="menu_order">' . $post->menu_order . '</div>';
     304    }
    304305
    305306    $taxonomy_names = get_object_taxonomies( $post->post_type );
     
    762763 *
    763764 * @since 1.5.0
    764  *
    765  * @param string $default Optional. The template file name. Default empty.
    766  */
    767 function page_template_dropdown( $default = '' ) {
    768     $templates = get_page_templates( get_post() );
     765 * @since 4.7.0 Added the `$post_type` parameter.
     766 *
     767 * @param string $default   Optional. The template file name. Default empty.
     768 * @param string $post_type Optional. Post type to get templates for. Default 'post'.
     769 */
     770function page_template_dropdown( $default = '', $post_type = 'page' ) {
     771    $templates = get_page_templates( null, $post_type );
    769772    ksort( $templates );
    770773    foreach ( array_keys( $templates ) as $template ) {
Note: See TracChangeset for help on using the changeset viewer.