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.

git-svn-id: https://develop.svn.wordpress.org/trunk@38951 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler
2016-10-26 08:06:43 +00:00
parent ee14487043
commit a9516abb54
18 changed files with 391 additions and 150 deletions
+3 -2
View File
@@ -259,8 +259,9 @@ foreach ( get_object_taxonomies( $post ) as $tax_name ) {
add_meta_box( $tax_meta_box_id, $label, $taxonomy->meta_box_cb, null, 'side', 'core', array( 'taxonomy' => $tax_name ) );
}
if ( post_type_supports($post_type, 'page-attributes') )
add_meta_box('pageparentdiv', 'page' == $post_type ? __('Page Attributes') : __('Attributes'), 'page_attributes_meta_box', null, 'side', 'core');
if ( post_type_supports( $post_type, 'page-attributes' ) || count( get_page_templates( null, $post_type ) ) > 0 ) {
add_meta_box( 'pageparentdiv', $post_type_object->labels->attributes, 'page_attributes_meta_box', null, 'side', 'core' );
}
if ( $thumbnail_support && current_user_can( 'upload_files' ) )
add_meta_box('postimagediv', esc_html( $post_type_object->labels->featured_image ), 'post_thumbnail_meta_box', null, 'side', 'low');
@@ -1528,31 +1528,28 @@ class WP_Posts_List_Table extends WP_List_Table {
<span class="input-text-wrap"><input type="text" name="menu_order" class="inline-edit-menu-order-input" value="<?php echo $post->menu_order ?>" /></span>
</label>
<?php endif; // !$bulk
if ( 'page' === $screen->post_type ) :
?>
<label>
<span class="title"><?php _e( 'Template' ); ?></span>
<select name="page_template">
<?php if ( $bulk ) : ?>
<option value="-1"><?php _e( '&mdash; No Change &mdash;' ); ?></option>
<?php endif; // $bulk ?>
<?php
/** This filter is documented in wp-admin/includes/meta-boxes.php */
$default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'quick-edit' );
?>
<option value="default"><?php echo esc_html( $default_title ); ?></option>
<?php page_template_dropdown() ?>
</select>
</label>
<?php
endif; // page post_type
endif; // !$bulk
endif; // page-attributes
?>
<?php if ( 0 < count( get_page_templates( null, $screen->post_type ) ) ) : ?>
<label>
<span class="title"><?php _e( 'Template' ); ?></span>
<select name="page_template">
<?php if ( $bulk ) : ?>
<option value="-1"><?php _e( '&mdash; No Change &mdash;' ); ?></option>
<?php endif; // $bulk ?>
<?php
/** This filter is documented in wp-admin/includes/meta-boxes.php */
$default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'quick-edit' );
?>
<option value="default"><?php echo esc_html( $default_title ); ?></option>
<?php page_template_dropdown( $post->page_template, $screen->post_type ) ?>
</select>
</label>
<?php endif; ?>
<?php if ( count( $flat_taxonomies ) && !$bulk ) : ?>
<?php foreach ( $flat_taxonomies as $taxonomy ) : ?>
+20 -14
View File
@@ -788,8 +788,7 @@ function post_revisions_meta_box( $post ) {
* @param object $post
*/
function page_attributes_meta_box($post) {
$post_type_object = get_post_type_object($post->post_type);
if ( $post_type_object->hierarchical ) {
if ( is_post_type_hierarchical( $post->post_type ) ) :
$dropdown_args = array(
'post_type' => $post->post_type,
'exclude_tree' => $post->ID,
@@ -812,16 +811,17 @@ function page_attributes_meta_box($post) {
*/
$dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post );
$pages = wp_dropdown_pages( $dropdown_args );
if ( ! empty($pages) ) {
if ( ! empty($pages) ) :
?>
<p><strong><?php _e('Parent') ?></strong></p>
<label class="screen-reader-text" for="parent_id"><?php _e('Parent') ?></label>
<?php echo $pages; ?>
<?php
} // end empty pages check
} // end hierarchical check.
if ( 'page' == $post->post_type && 0 != count( get_page_templates( $post ) ) && get_option( 'page_for_posts' ) != $post->ID ) {
$template = !empty($post->page_template) ? $post->page_template : false;
endif; // end empty pages check
endif; // end hierarchical check.
if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
$template = ! empty( $post->page_template ) ? $post->page_template : false;
?>
<p><strong><?php _e('Template') ?></strong><?php
/**
@@ -835,7 +835,13 @@ function page_attributes_meta_box($post) {
*/
do_action( 'page_attributes_meta_box_template', $template, $post );
?></p>
<label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
<label class="screen-reader-text" for="page_template">
<?php
$post_type_object = get_post_type_object( $post->post_type );
echo esc_html( $post_type_object->labels->attributes );
?>
</label>
<select name="page_template" id="page_template">
<?php
/**
* Filters the title of the default page template displayed in the drop-down.
@@ -849,16 +855,16 @@ function page_attributes_meta_box($post) {
$default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' );
?>
<option value="default"><?php echo esc_html( $default_title ); ?></option>
<?php page_template_dropdown($template); ?>
<?php page_template_dropdown( $template, $post->post_type ); ?>
</select>
<?php
} ?>
<?php endif; ?>
<?php if ( post_type_supports( $post->post_type, 'page-attributes' ) ) : ?>
<p><strong><?php _e('Order') ?></strong></p>
<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>
<?php if ( 'page' == $post->post_type && get_current_screen()->get_help_tabs() ) { ?>
<?php if ( 'page' == $post->post_type && get_current_screen()->get_help_tabs() ) : ?>
<p><?php _e( 'Need help? Use the Help tab above the screen title.' ); ?></p>
<?php
}
<?php endif;
endif;
}
// -- Link related Meta Boxes
+10 -7
View File
@@ -293,14 +293,15 @@ function get_inline_data($post) {
<div class="ss">' . mysql2date( 's', $post->post_date, false ) . '</div>
<div class="post_password">' . esc_html( $post->post_password ) . '</div>';
if ( $post_type_object->hierarchical )
if ( $post_type_object->hierarchical ) {
echo '<div class="post_parent">' . $post->post_parent . '</div>';
}
if ( $post->post_type == 'page' )
echo '<div class="page_template">' . esc_html( get_post_meta( $post->ID, '_wp_page_template', true ) ) . '</div>';
echo '<div class="page_template">' . esc_html( $post->page_template ) . '</div>';
if ( post_type_supports( $post->post_type, 'page-attributes' ) )
if ( post_type_supports( $post->post_type, 'page-attributes' ) ) {
echo '<div class="menu_order">' . $post->menu_order . '</div>';
}
$taxonomy_names = get_object_taxonomies( $post->post_type );
foreach ( $taxonomy_names as $taxonomy_name) {
@@ -761,11 +762,13 @@ function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) {
* Print out option HTML elements for the page templates drop-down.
*
* @since 1.5.0
* @since 4.7.0 Added the `$post_type` parameter.
*
* @param string $default Optional. The template file name. Default empty.
* @param string $default Optional. The template file name. Default empty.
* @param string $post_type Optional. Post type to get templates for. Default 'post'.
*/
function page_template_dropdown( $default = '' ) {
$templates = get_page_templates( get_post() );
function page_template_dropdown( $default = '', $post_type = 'page' ) {
$templates = get_page_templates( null, $post_type );
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) {
$selected = selected( $default, $templates[ $template ], false );
+5 -3
View File
@@ -102,12 +102,14 @@ function delete_theme($stylesheet, $redirect = '') {
* Get the Page Templates available in this theme
*
* @since 1.5.0
* @since 4.7.0 Added the `$post_type` parameter.
*
* @param WP_Post|null $post Optional. The post being edited, provided for context.
* @param WP_Post|null $post Optional. The post being edited, provided for context.
* @param string $post_type Optional. Post type to get the templates for. Default 'page'.
* @return array Key is the template name, value is the filename of the template
*/
function get_page_templates( $post = null ) {
return array_flip( wp_get_theme()->get_page_templates( $post ) );
function get_page_templates( $post = null, $post_type = 'page' ) {
return array_flip( wp_get_theme()->get_page_templates( $post, $post_type ) );
}
/**