How to Setup Template Files for Custom Post Types
Hi all,
I thought I would share the steps for creating templates for custom post types. Hopefully I have everything here and it is clear enough.
Creating Templates for Custom Post Types Using Pippins Easy Custom Posts Types Plugin
I use Easy Custom Post Types to set up a Treatments cpt and a custom taxonomy Conditions.
First I want to create my templates.
In your WordPress Dashboard go to Content Types --> Settings
Select the templates you want to enable.

In your theme look for the templates created.

Each template contains:
<?php
/**- @package Master
- @author YOOtheme http://www.yootheme.com
- @copyright Copyright (C) YOOtheme GmbH
- @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*/
// get warp
$warp = Warp::getInstance();// load main template file, located in /layouts/template.php
echo $warp['template']->render('template');
If you aren't using the plugin, just create your archive, single and/or taxonomy templates using the code above.
- Now you need to create those templates in styles --> (style name) --> layouts.
I copied single.php and archive.php from warp --> systems --> wordpress --> layouts and put them into my styles --> (style name) --> layouts.
single-treatments.php has no changes
archive-treatments.php - the line query_posts calls my post type treatments.
<div id="system">
<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
global $query_string;
query_posts($query_string . '&post_type=treatments&posts_per_page=-10&paged=' . $paged);
if (have_posts()) : ?>
There is no category.php file to copy so I created my own taxonomy-conditions.php. Under the div I call the term for my custom taxonomy and then I created a if/while loop to get the posts.
<div id="system">
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
<h1 class="title"><?php printf(__("%s Treatments", "warp"), '<span>' . $term->name . '</span>' ); ?></h1>
<?php
if ( have_posts() ) : while (have_posts()) : the_post();?>
<?php echo $this->render('_post'); ?>
<?php echo $this->render("_pagination", array("type"=>"posts")); ?>
<?php endwhile; ?>
<?php else : ?>
<h1 class="title"><?php printf(__("Sorry, but there aren't any posts in the %s category yet.", "warp"), '<span>' . $term->name . '</span>' ); ?></h1>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
<?php echo $this->render('_post'); ?>
<?php echo $this->render("_pagination", array("type"=>"posts")); ?>
<?php endwhile; ?>
<?php else : ?>
<h1 class="title"><?php printf(__("Sorry, but there aren't any posts in the %s category yet.", "warp"), '<span>' . $term->name . '</span>' ); ?></h1>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
Next I added the templates to content.php. This is where I get the post type and the custom taxonomy.
$content = ''; if (is_home()) { $content = 'index'; } elseif (is_page()) { $content = 'page'; } elseif (is_attachment()) { $content = 'attachment'; }elseif ((is_single()) && (get_post_type() == 'treatments')) { $content = 'single-treatments'; } elseif (is_single()) { $content = 'single'; } elseif (is_search()) { $content = 'search'; } elseif ((is_archive()) && is_post_type_archive( 'treatments')) { $content = 'archive-treatments'; } elseif ((is_archive()) && get_taxonomy( 'conditions')) { $content = 'taxonomy-conditions'; } elseif (is_archive() && is_author()) { $content = 'author'; } elseif (is_archive()) { $content = 'archive'; } elseif (is_404()) { $content = '404'; }
I hope I have everything here. Next up will be how to use Widget Options with custom post type templates and taxonomies.
Edited
