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

    r25409 r38951  
    4949        switch_theme( $theme['Template'], $theme['Stylesheet'] );
    5050
    51         $templates = get_page_templates();
    52         $this->assertCount( 3, $templates );
    53         $this->assertEquals( "template-top-level.php", $templates['Top Level'] );
    54         $this->assertEquals( "subdir/template-sub-dir.php", $templates['Sub Dir'] );
    55         $this->assertEquals( "template-header.php", $templates['This Template Header Is On One Line'] );
     51        $this->assertEqualSetsWithIndex( array(
     52            'Top Level'                           => 'template-top-level.php',
     53            'Sub Dir'                             => 'subdir/template-sub-dir.php',
     54            'This Template Header Is On One Line' => 'template-header.php',
     55        ), get_page_templates() );
    5656
    5757        $theme = wp_get_theme( 'page-templates' );
     
    6060        switch_theme( $theme['Template'], $theme['Stylesheet'] );
    6161
    62         $templates = get_page_templates();
    63         $this->assertCount( 3, $templates );
    64         $this->assertEquals( "template-top-level.php", $templates['Top Level'] );
    65         $this->assertEquals( "subdir/template-sub-dir.php", $templates['Sub Dir'] );
    66         $this->assertEquals( "template-header.php", $templates['This Template Header Is On One Line'] );
     62        $this->assertEqualSetsWithIndex( array(
     63            'Top Level'                           => 'template-top-level.php',
     64            'Sub Dir'                             => 'subdir/template-sub-dir.php',
     65            'This Template Header Is On One Line' => 'template-header.php',
     66        ), get_page_templates() );
     67    }
     68
     69    /**
     70     * @ticket 18375
     71     */
     72    function test_page_templates_different_post_types() {
     73        $theme = wp_get_theme( 'page-templates' );
     74        $this->assertNotEmpty( $theme );
     75
     76        switch_theme( $theme['Template'], $theme['Stylesheet'] );
     77
     78        $this->assertEqualSetsWithIndex( array(
     79            'Top Level' => 'template-top-level-post-types.php',
     80            'Sub Dir'   => 'subdir/template-sub-dir-post-types.php',
     81        ), get_page_templates( null, 'foo' ) );
     82        $this->assertEqualSetsWithIndex( array(
     83            'Top Level' => 'template-top-level-post-types.php',
     84            'Sub Dir'   => 'subdir/template-sub-dir-post-types.php',
     85        ), get_page_templates( null, 'post' ) );
     86        $this->assertEquals( array(), get_page_templates( null, 'bar' ) );
    6787    }
    6888}
Note: See TracChangeset for help on using the changeset viewer.