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/meta-boxes.php

    r38880 r38951  
    789789 */
    790790function page_attributes_meta_box($post) {
    791     $post_type_object = get_post_type_object($post->post_type);
    792     if ( $post_type_object->hierarchical ) {
     791    if ( is_post_type_hierarchical( $post->post_type ) ) :
    793792        $dropdown_args = array(
    794793            'post_type'        => $post->post_type,
     
    813812        $dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post );
    814813        $pages = wp_dropdown_pages( $dropdown_args );
    815         if ( ! empty($pages) ) {
     814        if ( ! empty($pages) ) :
    816815?>
    817816<p><strong><?php _e('Parent') ?></strong></p>
     
    819818<?php echo $pages; ?>
    820819<?php
    821         } // end empty pages check
    822     } // end hierarchical check.
    823     if ( 'page' == $post->post_type && 0 != count( get_page_templates( $post ) ) && get_option( 'page_for_posts' ) != $post->ID ) {
    824         $template = !empty($post->page_template) ? $post->page_template : false;
     820        endif; // end empty pages check
     821    endif;  // end hierarchical check.
     822
     823    if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
     824        $template = ! empty( $post->page_template ) ? $post->page_template : false;
    825825        ?>
    826826<p><strong><?php _e('Template') ?></strong><?php
     
    836836    do_action( 'page_attributes_meta_box_template', $template, $post );
    837837?></p>
    838 <label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
     838<label class="screen-reader-text" for="page_template">
     839    <?php
     840    $post_type_object = get_post_type_object( $post->post_type );
     841    echo esc_html( $post_type_object->labels->attributes );
     842    ?>
     843</label>
     844<select name="page_template" id="page_template">
    839845<?php
    840846/**
     
    850856?>
    851857<option value="default"><?php echo esc_html( $default_title ); ?></option>
    852 <?php page_template_dropdown($template); ?>
     858<?php page_template_dropdown( $template, $post->post_type ); ?>
    853859</select>
    854 <?php
    855     } ?>
     860<?php endif; ?>
     861<?php if ( post_type_supports( $post->post_type, 'page-attributes' ) ) : ?>
    856862<p><strong><?php _e('Order') ?></strong></p>
    857863<p><label class="screen-reader-text" for="menu_order"><?php _e('Order') ?></label><input name="menu_order" type="text" size="4" id="menu_order" value="<?php echo esc_attr($post->menu_order) ?>" /></p>
    858 <?php if ( 'page' == $post->post_type && get_current_screen()->get_help_tabs() ) { ?>
     864<?php if ( 'page' == $post->post_type && get_current_screen()->get_help_tabs() ) : ?>
    859865<p><?php _e( 'Need help? Use the Help tab above the screen title.' ); ?></p>
    860 <?php
    861     }
     866<?php endif;
     867    endif;
    862868}
    863869
Note: See TracChangeset for help on using the changeset viewer.