Make WordPress Core


Ignore:
Timestamp:
10/26/2016 08:06:43 AM (8 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/post-template.php

    r38798 r38951  
    595595        $classes[] = 'error404';
    596596
    597     if ( is_single() ) {
     597    if ( is_singular() ) {
    598598        $post_id = $wp_query->get_queried_object_id();
    599599        $post = $wp_query->get_queried_object();
    600 
    601         $classes[] = 'single';
    602         if ( isset( $post->post_type ) ) {
    603             $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
    604             $classes[] = 'postid-' . $post_id;
    605 
    606             // Post Format
    607             if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
    608                 $post_format = get_post_format( $post->ID );
    609 
    610                 if ( $post_format && !is_wp_error($post_format) )
    611                     $classes[] = 'single-format-' . sanitize_html_class( $post_format );
    612                 else
    613                     $classes[] = 'single-format-standard';
     600        $post_type = $post->post_type;
     601
     602        if ( is_page_template() ) {
     603            $classes[] = "{$post_type}-template";
     604
     605            $template_slug  = get_page_template_slug( $post_id );
     606            $template_parts = explode( '/', $template_slug );
     607
     608            foreach ( $template_parts as $part ) {
     609                $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
     610            }
     611            $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
     612        } else {
     613            $classes[] = "{$post_type}-template-default";
     614        }
     615
     616        if ( is_single() ) {
     617            $classes[] = 'single';
     618            if ( isset( $post->post_type ) ) {
     619                $classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id );
     620                $classes[] = 'postid-' . $post_id;
     621
     622                // Post Format
     623                if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
     624                    $post_format = get_post_format( $post->ID );
     625
     626                    if ( $post_format && !is_wp_error($post_format) )
     627                        $classes[] = 'single-format-' . sanitize_html_class( $post_format );
     628                    else
     629                        $classes[] = 'single-format-standard';
     630                }
    614631            }
    615632        }
     
    620637            $classes[] = 'attachmentid-' . $post_id;
    621638            $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
     639        } elseif ( is_page() ) {
     640            $classes[] = 'page';
     641
     642            $page_id = $wp_query->get_queried_object_id();
     643
     644            $post = get_post($page_id);
     645
     646            $classes[] = 'page-id-' . $page_id;
     647
     648            if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) {
     649                $classes[] = 'page-parent';
     650            }
     651
     652            if ( $post->post_parent ) {
     653                $classes[] = 'page-child';
     654                $classes[] = 'parent-pageid-' . $post->post_parent;
     655            }
    622656        }
    623657    } elseif ( is_archive() ) {
     
    671705                $classes[] = 'term-' . $term->term_id;
    672706            }
    673         }
    674     } elseif ( is_page() ) {
    675         $classes[] = 'page';
    676 
    677         $page_id = $wp_query->get_queried_object_id();
    678 
    679         $post = get_post($page_id);
    680 
    681         $classes[] = 'page-id-' . $page_id;
    682 
    683         if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) {
    684             $classes[] = 'page-parent';
    685         }
    686 
    687         if ( $post->post_parent ) {
    688             $classes[] = 'page-child';
    689             $classes[] = 'parent-pageid-' . $post->post_parent;
    690         }
    691         if ( is_page_template() ) {
    692             $classes[] = 'page-template';
    693 
    694             $template_slug  = get_page_template_slug( $page_id );
    695             $template_parts = explode( '/', $template_slug );
    696 
    697             foreach ( $template_parts as $part ) {
    698                 $classes[] = 'page-template-' . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
    699             }
    700             $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
    701         } else {
    702             $classes[] = 'page-template-default';
    703707        }
    704708    }
     
    16221626 * @since 2.5.0
    16231627 * @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates.
     1628 * @since 4.7.0 Now works with any post type, not just pages.
    16241629 *
    16251630 * @param string|array $template The specific template name or array of templates to match.
     
    16271632 */
    16281633function is_page_template( $template = '' ) {
    1629     if ( ! is_page() )
    1630         return false;
    1631 
    16321634    $page_template = get_page_template_slug( get_queried_object_id() );
    16331635
     
    16501652
    16511653/**
    1652  * Get the specific template name for a page.
     1654 * Get the specific template name for a given post.
    16531655 *
    16541656 * @since 3.4.0
    1655  *
    1656  * @param int $post_id Optional. The page ID to check. Defaults to the current post, when used in the loop.
     1657 * @since 4.7.0 Now works with any post type, not just pages.
     1658 *
     1659 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
    16571660 * @return string|false Page template filename. Returns an empty string when the default page template
    1658  *  is in use. Returns false if the post is not a page.
    1659  */
    1660 function get_page_template_slug( $post_id = null ) {
    1661     $post = get_post( $post_id );
    1662     if ( ! $post || 'page' != $post->post_type )
     1661 *  is in use. Returns false if the post does not exist.
     1662 */
     1663function get_page_template_slug( $post = null ) {
     1664    $post = get_post( $post );
     1665
     1666    if ( ! $post ) {
    16631667        return false;
     1668    }
     1669
    16641670    $template = get_post_meta( $post->ID, '_wp_page_template', true );
    1665     if ( ! $template || 'default' == $template )
     1671
     1672    if ( ! $template || 'default' == $template ) {
    16661673        return '';
     1674    }
     1675
    16671676    return $template;
    16681677}
Note: See TracChangeset for help on using the changeset viewer.