This is probably the single most valuable plugin to date (exaggeration?). It basically checks to see if a post in a certain category, and then assigns that post a separate custom template. Genius. And it doesn’t stop there. It also, allows you to assign a custom template to any post ID which means you can make one specific page look completely different than all of the others. Extremely powerful plugin. I found this over at wpelements.com in an old feed article. The plugin was created by Ryan Boren way back in 2005 and it is still very much effective today.
Here’s the code. Enjoy.
<?php
/*
Plugin Name: Custom Post Templates
Plugin URI: http://boren.nu/
Description: Load custom single post templates.
Author: Ryan Boren
Version: 0.9
Author URI: http://boren.nu/
*/
function cpt_custom_post_template($template) {
global $wp_query;
$post = $wp_query->post;
$id = $post->ID;
// If a template exists for this post ID, load it.
if ( file_exists(TEMPLATEPATH . "/single-{$id}.php") )
return TEMPLATEPATH . "/single-{$id}.php";
// Add custom checks here. For example, give posts different templates
// depending on what categories they are in.
if ( in_category('1') && file_exists(TEMPLATEPATH . '/single-cat-1.php') )
return TEMPLATEPATH . '/single-cat-1.php';
return $template;
}
add_filter('single_template', 'cpt_custom_post_template');
?>
7 Comments
dumb question…where do I post this code?
Thanks.
c
You’ll need to make a new PHP file and paste the code into it. Then just upload it to your plugins folder like any other plugin and activate it.
thanks for your help!
btw. I’m loving your Neutica theme, being a type junkie and after looking at some of your work you have a real eye for good design/typography. Definitely a lost art.
cheers,
c
Thanks Cee Ho!
Saved me some trouble, thanks much!
I’ve modified the plug-in, to support dynamic categories, instead of relying on custom checks.
post;
$id = $post->ID;
$templatePath = get_template_directory();
// If a template exists for this post ID, load it.
if (file_exists("$templatePath/single-$id.php")) {
return "$templatePath/single-$id.php";
}
// If a template exists for this post's category ID, load it.
$categories = get_the_category($id);
// TODO: ascend parents for hierarchical single cat templates
foreach ($categories as $cat) {
$catId = $cat->cat_ID;
if (file_exists("$templatePath/single-cat-$catId.php")) {
return "$templatePath/single-cat-$catId.php";
}
}
return $template;
}
add_filter('single_template', 'cpt_custom_post_template');
?>
I’ve modified the plug-in, to support dynamic categories, instead of relying on custom checks.
/*
Plugin Name: Custom Post Templates
Plugin URI: http://boren.nu/
Description: Load custom single post templates.
Author: Ryan Boren
Author URI: http://boren.nu/
Author: Lon F. Binder
Author URI: http://www.foodmayhem.com/
Version: 0.9.1
*/
function cpt_custom_post_template($template) {global $wp_query;
$post = $wp_query->post;
$id = $post->ID;
$templatePath = get_template_directory();
// If a template exists for this post ID, load it.
if (file_exists("$templatePath/single-$id.php")) {
return "$templatePath/single-$id.php";
}
// If a template exists for this post's category ID, load it.
$categories = get_the_category($id);
// TODO: ascend parents for hierarchical single cat templates
foreach ($categories as $cat) {
$catId = $cat->cat_ID;
if (file_exists("$templatePath/single-cat-$catId.php")) {
return "$templatePath/single-cat-$catId.php";
}
}
return $template;
}
add_filter('single_template', 'cpt_custom_post_template');