Created
June 10, 2013 13:15
-
-
Save samediamba/5748620 to your computer and use it in GitHub Desktop.
This is a Gist courtesy of wpbeginner.com (http://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** To use a separate single post template for each category, you will need to add the following function in your functions.php: **/ | |
/** | |
* Define a constant path to our single template folder | |
*/ | |
define(SINGLE_PATH, TEMPLATEPATH . '/single'); | |
/** | |
* 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 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment