Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how do I Setup templates for WordPress 3.0 Custom Post Type?

Posted on 2011-02-16
3
Medium Priority
?
281 Views
Last Modified: 2012-05-11
I created a post type called Surgeons and I want to set a spacific template for this page type.
It looks like it is just using single.php as the template. How do I change this to a spacific one of my choosing?

Here is the code I added to the functions.php
add_action( 'init', 'create_my_post_types' );    

function create_my_post_types() {
    register_post_type( 'Surgeons',
        array(
            'labels' => array(
                'name' => __( 'Surgeons' ),
                'singular_name' => __( 'List Surgeons' )
            ),
            'public' => true,
            'hierarchical' => true,
            'show_ui' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'menu_position' => 5,
            'supports' => array( 'title', 'editor', 'comments', 'trackbacks', 'author', 'excerpt', 'custom-fields', 'thumbnail' ),
            'rewrite' => array( 'slug' => 'mypage', 'with_front' => false ),
        )
    );
}

Open in new window

0
Comment
Question by:jbrashear72
  • 2
3 Comments
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 34910684
Copy the index.php file and paste it into a text editorl
Add this to the very top:
<?php
/*
Template Name: Surgeons
*/
?>

Open in new window

Then name the file as surgeons.php and add it to your theme folder. You will now have the option to use this page template from the page editor page type drop menu.
0
 
LVL 3

Author Comment

by:jbrashear72
ID: 34910768
But this is a post not a page. the drop down for templates does not show up.

I was told that if I added a file called single-Surgeons.php that this would work but that is being ignored too.
0
 
LVL 23

Accepted Solution

by:
jeremyjared74 earned 2000 total points
ID: 34911683
OK for that you need to do this:

Create a custom page template like I suggested earlier, but put this in the top of the page instead:
<?php
/*
Single Post Template: Surgeons
Description: This part is optional, but helpful for describing the Post Template
*/
?>

Open in new window


Then you need to add this to your single.php:
    <?php
    $post = $wp_query->post;
    if ( in_category('10') ) {include(TEMPLATEPATH . '/singleOranges.php');}
    elseif ( in_category('11') ) {include(TEMPLATEPATH . '/singleApples.php');}
    elseif ( in_category('12') ) {include(TEMPLATEPATH . '/singleGrapes.php');}
    else {include(TEMPLATEPATH . '/singleOriginal.php');
    }
    ?>

Open in new window

Then upload your new template page to your theme file. Now your posts will change templates depending on the category it is in.

NOTE: in_category("10") is where you need to add the category that will hold the posts for the template you want to use this in. So you might want to create a category for all posts that you want styled this way, then get the category id and place it where the 10 is. I added a few more template choices (11, 12) in case you wanted more. If you don't, then just give them a number like 9999 or 9998 so they don't get used.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Find out what you should include to make the best professional email signature for your organization.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question