Solved

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

Posted on 2011-02-16
3
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

617 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