how to attach a page to a custom post type

This has worked for me in the past, so I am unsure why it is not working now.

I have created a custom post type:

Read More
add_action('init', 'register_team');   
function register_team(){
        $args = array(
            'label' => __('Design Team'),
            'singular_label' => __('Design Team'),
            'public' => true,
            'show_ui' => true,
            'capability_type' => 'post',
            'hierarchical' => true,
            'rewrite' => array("slug" => "design-team",'with_front' => true), // Permalinks format
            'supports' => array( 'title', 'editor', 'thumbnail' ),
            'add_new' => __( 'Add New Member' ),
            'add_new_item' => __( 'Add New Member' ),
            'edit' => __( 'Edit Member' ),
            'edit_item' => __( 'Edit Member' ),
            'new_item' => __( 'New Member' ),
            'view' => __( 'View Member' ),
            'view_item' => __( 'View Member' ),
            'search_items' => __( 'Search Design Team' ),
            'not_found' => __( 'No info found' ),
            'not_found_in_trash' => __( 'No info found in Trash' ),
            'parent' => __( 'Parent Info' ),
            'menu_position' =>__( 7 ),
           );

        register_post_type( 'team' , $args );
    }

and called the function which I can see in the CMS, add new entries, etc. I need to attach a page template to this custom post type. On the same site, I have created a custom post type named showroom, and attached the custom post type to a page by creating a file called page-showroom.php. However, when I create a file called page-team.php, it will not associate to this page. Is this a syntax issue?

UPDATE
I got around this by creating a page in the CMS, and adding the template using the Page Attributes. The reason I do not particularly like this solution is due to the possibility a user could change the template of the page, causing it to no longer work.

I just feel like I am missing something relative to how the WP Core defines page-?? variable template names or it is a typo, stupid mistake, etc…

UPDATE
As requested, here is the code from functions.php which loads all of my CPT’s

// CUSTOM POST TYPES
add_action('init', 'register_showroom');
add_action('init', 'register_project_gallery');
add_action('init', 'register_slideshow');
add_action('init', 'register_team');


// ADD Showroom
function register_showroom(){
    $args = array(
        'label' => __('Showroom'),
        'singular_label' => __('Showroom'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'rewrite' => array("slug" => "showroom",'with_front' => true), // Permalinks format
        'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'page-attributes' ),
        'add_new' => __( 'Add New' ),
        'add_new_item' => __( 'Add New' ),
        'edit' => __( 'Edit' ),
        'edit_item' => __( 'Edit' ),
        'new_item' => __( 'New' ),
        'view' => __( 'View' ),
        'view_item' => __( 'View' ),
        'search_items' => __( 'Search Showroom' ),
        'not_found' => __( 'No info found' ),
        'not_found_in_trash' => __( 'No info found in Trash' ),
        'parent' => __( 'Parent Info' ),
        'menu_position' =>__( 4 ),
       );

    register_post_type( 'showroom' , $args );
}


// ADD Project Gallery
function register_project_gallery(){
    $args = array(
        'label' => __('Project Gallery'),
        'singular_label' => __('Project Gallery'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'rewrite' => array("slug" => "project-gallery",'with_front' => true), // Permalinks format
        'supports' => array( 'title', 'editor', 'thumbnail' ),
        'add_new' => __( 'Add New' ),
        'add_new_item' => __( 'Add New' ),
        'edit' => __( 'Edit' ),
        'edit_item' => __( 'Edit' ),
        'new_item' => __( 'New' ),
        'view' => __( 'View' ),
        'view_item' => __( 'View' ),
        'search_items' => __( 'Search Project Gallery' ),
        'not_found' => __( 'No info found' ),
        'not_found_in_trash' => __( 'No info found in Trash' ),
        'parent' => __( 'Parent Info' ),
        'menu_position' =>__( 5 ),
       );

    register_post_type( 'project_gallery' , $args );
}

// ADD Slideshow
function register_slideshow(){
    $args = array(
        'label' => __('Homepage Slideshow'),
        'singular_label' => __('Homepage Slideshow'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'rewrite' => array("slug" => "project-gallery",'with_front' => true), // Permalinks format
        'supports' => array( 'title', 'excerpt', 'thumbnail' ),
        'add_new' => __( 'Add New Slide' ),
        'add_new_item' => __( 'Add New Slide' ),
        'edit' => __( 'Edit' ),
        'edit_item' => __( 'Edit' ),
        'new_item' => __( 'New' ),
        'view' => __( 'View' ),
        'view_item' => __( 'View' ),
        'search_items' => __( 'Search Homepage Slideshow' ),
        'not_found' => __( 'No info found' ),
        'not_found_in_trash' => __( 'No info found in Trash' ),
        'parent' => __( 'Parent Info' ),
        'menu_position' =>__( 6 ),
       );

    register_post_type( 'slideshow' , $args );
}

// ADD Design Team
function register_team(){
    $args = array(
        'label' => __('Design Team'),
        'singular_label' => __('Design Team'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'rewrite' => array("slug" => "design-team",'with_front' => true), // Permalinks format
        'supports' => array( 'title', 'editor', 'thumbnail' ),
        'add_new' => __( 'Add New Member' ),
        'add_new_item' => __( 'Add New Member' ),
        'edit' => __( 'Edit Member' ),
        'edit_item' => __( 'Edit Member' ),
        'new_item' => __( 'New Member' ),
        'view' => __( 'View Member' ),
        'view_item' => __( 'View Member' ),
        'search_items' => __( 'Search Design Team' ),
        'not_found' => __( 'No info found' ),
        'not_found_in_trash' => __( 'No info found in Trash' ),
        'parent' => __( 'Parent Info' ),
        'menu_position' =>__( 7 ),
       );

    register_post_type( 'team' , $args );
} 

So I can successfully create a page-showroom.php, page-project_gallery.php, single-project_gallery.php, single-showroom.php which automatically attach themselves to the correct CPT but if I create page-team.php, it just loads the page.php.

Here is a sample of page-showroom.php, which works:

<?php /*  Template Name: Showroom   */ ?>

<?php get_header(); ?>

    <div id="primary" class="site-content showroom">
        <div id="content" role="main">

            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', 'showroom' ); ?>
            <?php endwhile; // end of the loop. ?>

        </div><!-- #content -->
    </div><!-- #primary -->

</div>
<?php get_footer(); ?>

and page-team.php, which does not work

<?php /*  Template Name: Team   */ ?>

<?php get_header(); ?>

    <div id="primary" class="site-content team">
        <div id="content" role="main">

            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', 'team' ); ?>
                <?php //comments_template( '', true ); ?>
            <?php endwhile; // end of the loop. ?>



        </div><!-- #content -->
    </div><!-- #primary -->

</div>
<?php get_footer(); ?>

Related posts

Leave a Reply

2 comments

  1. I usually just follow the template hierarchy. In your case — and I’m assuming you want your page to list all posts where post type = team — this would mean creating a page called “archive-team.php” under your “page-templates” directory. Alternatively, if you just want to display single posts, you would use “single-team.php” instead. This is the way you’re supposed to do it, at least. I do it this way and it works for me.

    http://codex.wordpress.org/Template_Hierarchy