Justin Tadlock to the rescue again, with a very clear post on creating single post templates in WordPress.

A single post template allows for some p[osts to function differently from others. They should not be used to change the look (style, design) of a particular post though. You can do this with CSS. They should only be used to change how a particular post functions.

This solution depends on creating a functions.php file in the child theme directory, and placing the post templates on a new folder called singles/.

I used this technique on the Mountain State Rotary E-Club site for the meeting posts, like this:


/**
* Filter the single_template with our custom function
*/
define(SINGLE_PATH, STYLESHEETPATH . '/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;

/**
* 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;
}

Then I named the template single-cat-meetings.php.

Random Posts

 

Comments are closed.