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/post/getBodyClass.php

    r37249 r38951  
    9393    }
    9494
     95    public function test_page_template_body_classes_no_template() {
     96        $post_id = self::factory()->post->create( array(
     97            'post_type' => 'page',
     98        ) );
     99        $this->go_to( get_permalink( $post_id ) );
     100
     101        $class = get_body_class();
     102
     103        $this->assertNotContains( 'page-template', $class );
     104        $this->assertContains( 'page-template-default', $class );
     105    }
     106
     107    public function test_page_template_body_classes() {
     108        $post_id = self::factory()->post->create( array(
     109            'post_type' => 'page',
     110        ) );
     111
     112        add_post_meta( $post_id, '_wp_page_template', 'templates/cpt.php' );
     113
     114        $this->go_to( get_permalink( $post_id ) );
     115
     116        $class = get_body_class();
     117
     118        $this->assertContains( 'page-template', $class );
     119        $this->assertContains( 'page-template-templates', $class );
     120        $this->assertContains( 'page-template-cpt', $class );
     121        $this->assertContains( 'page-template-templatescpt-php', $class );
     122    }
     123
     124    /**
     125     * @ticket 18375
     126     */
     127    public function test_page_template_body_classes_attachment() {
     128        $post_id = self::factory()->post->create( array(
     129            'post_type' => 'attachment',
     130        ) );
     131
     132        add_post_meta( $post_id, '_wp_page_template', 'templates/cpt.php' );
     133
     134        $this->go_to( get_permalink( $post_id ) );
     135
     136        $class = get_body_class();
     137
     138        $this->assertContains( 'attachment-template', $class );
     139        $this->assertContains( 'attachment-template-templates', $class );
     140        $this->assertContains( 'attachment-template-cpt', $class );
     141        $this->assertContains( 'attachment-template-templatescpt-php', $class );
     142    }
     143
     144    /**
     145     * @ticket 18375
     146     */
     147    public function test_page_template_body_classes_post() {
     148        $post_id = self::factory()->post->create();
     149
     150        add_post_meta( $post_id, '_wp_page_template', 'templates/cpt.php' );
     151
     152        $this->go_to( get_permalink( $post_id ) );
     153
     154        $class = get_body_class();
     155
     156        $this->assertContains( 'post-template', $class );
     157        $this->assertContains( 'post-template-templates', $class );
     158        $this->assertContains( 'post-template-cpt', $class );
     159        $this->assertContains( 'post-template-templatescpt-php', $class );
     160    }
    95161}
Note: See TracChangeset for help on using the changeset viewer.