Today we are going to present a unique tutorial that will help you to create a unique single post page for every category you wanted.
Every time we struggle behind single post file being common and not being able to present our content in a way that is suitable to our audience as well as satisfactory to ourselves. Lets go step by step
1. Open up your “functions.php” file. I simply love this file you can create wonders with this file. Paste the following magical code in it. But do take care of opening and closing of
<?php...?>
tags. They could hurt sometimes.
<?php
/**
* Define a constant path to our single template folder
*/
define(SINGLE_PATH, TEMPLATEPATH . '');
/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');
/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;
/**
* Checks for single template by ID
*/
if(file_exists(SINGLE_PATH . '/single-' . $post->ID . '.php'))
return SINGLE_PATH . '/single-' . $post->ID . '.php';
/**
* Checks for single template by category
* Check by category slug and ID
*/
foreach((array)get_the_category() as $cat) :
if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';
elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
endforeach;
/**
* Checks for default single post files within the single folder
*/
if(file_exists(SINGLE_PATH . '/single.php'))
return SINGLE_PATH . '/single.php';
elseif(file_exists(SINGLE_PATH . '/default.php'))
return SINGLE_PATH . '/default.php';
return $single;
}
?>
Now, How to use this magical piece within your wordpress file.
3. Suppose you have a category named as “Videos” and another category as “Music”. Both require different representation at details pages. Now create an .php file with name as “single-cat-videos.php”. This file will serve details page of all posts that are from “videos” category. You can perform same for music category and have a file that serves only single file from Music section as well.



Tue, Oct 25, 2011
CMS tutorials, Wordpress tutorials