Solved

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

Posted on 2011-02-16
3
273 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 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now