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/tests/phpunit/tests/template.php

    r38583 r38951  
    3737        ) );
    3838        set_post_format( self::$post, 'quote' );
     39        add_post_meta( self::$post->ID, '_wp_page_template', 'templates/post.php' );
    3940    }
    4041
     
    204205    }
    205206
     207    /**
     208     * @ticket 18375
     209     */
    206210    public function test_single_template_hierarchy_for_post() {
    207211        $this->assertTemplateHierarchy( get_permalink( self::$post ), array(
     212            'templates/post.php',
    208213            'single-post-post-name-😀.php',
    209214            'single-post-post-name-%f0%9f%98%80.php',
     
    221226
    222227        $this->assertTemplateHierarchy( get_permalink( $cpt ), array(
     228            'single-cpt-cpt-name-😀.php',
     229            'single-cpt-cpt-name-%f0%9f%98%80.php',
     230            'single-cpt.php',
     231            'single.php',
     232            'singular.php',
     233        ) );
     234    }
     235
     236    /**
     237     * @ticket 18375
     238     */
     239    public function test_single_template_hierarchy_for_custom_post_type_with_template() {
     240        $cpt = self::factory()->post->create_and_get( array(
     241            'post_type' => 'cpt',
     242            'post_name' => 'cpt-name-😀',
     243        ) );
     244        add_post_meta( $cpt->ID, '_wp_page_template', 'templates/cpt.php' );
     245
     246        $this->assertTemplateHierarchy( get_permalink( $cpt ), array(
     247            'templates/cpt.php',
    223248            'single-cpt-cpt-name-😀.php',
    224249            'single-cpt-cpt-name-%f0%9f%98%80.php',
     
    248273    }
    249274
     275    /**
     276     * @ticket 18375
     277     */
     278    public function test_attachment_template_hierarchy_with_template() {
     279        $attachment = self::factory()->attachment->create_and_get( array(
     280            'post_name'      => 'attachment-name-😀',
     281            'file'           => 'image.jpg',
     282            'post_mime_type' => 'image/jpeg',
     283        ) );
     284
     285        add_post_meta( $attachment, '_wp_page_template', 'templates/cpt.php' );
     286
     287        $this->assertTemplateHierarchy( get_permalink( $attachment ), array(
     288            'image-jpeg.php',
     289            'jpeg.php',
     290            'image.php',
     291            'attachment.php',
     292            'single-attachment-attachment-name-😀.php',
     293            'single-attachment-attachment-name-%f0%9f%98%80.php',
     294            'single-attachment.php',
     295            'single.php',
     296            'singular.php',
     297        ) );
     298    }
     299
    250300    public function test_embed_template_hierarchy_for_post() {
    251301        $this->assertTemplateHierarchy( get_post_embed_url( self::$post ), array(
     
    253303            'embed-post.php',
    254304            'embed.php',
     305            'templates/post.php',
    255306            'single-post-post-name-😀.php',
    256307            'single-post-post-name-%f0%9f%98%80.php',
Note: See TracChangeset for help on using the changeset viewer.