Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've used Justin Tadlock's great function for category specific custom post templates many times, where you can use single-cat-id.php for single posts belonging to a specific category. I just ran into a problem though working on a new site that needs to use the single-{posttype} template for a custom post type. The function seems to prevent the custom posts from using single-{posttype} template.

Here's the applicable code:

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

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

Any thoughts on how to modify the following code to allow both the original function of single-cat-id as well as single-{posttype}?

share|improve this question
    
Hi Chris, please be sure check out the editting help page for help with formatting your questions. –  t31os Apr 3 '11 at 23:06
    
Thanks for fixing that up. –  Christopher Apr 4 '11 at 0:59
    
You're welcome... :) –  t31os Apr 4 '11 at 1:09

1 Answer 1

up vote 1 down vote accepted

You can check for your post type single-{post-type}.php first try this:

/* 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 post type */
if ($post->post_type == "POST TYPE NAME"){
    if(file_exists(SINGLE_PATH . '/single-' . $post->post_type . '.php'))
        return SINGLE_PATH . '/single-' . $post->post_type . '.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; 
}

and just replace "POST TYPE NAME" with your custom post type name.

share|improve this answer
    
Perfect! I knew it was something like this but couldn't quite get it worked out. Many thanks! –  Christopher Apr 4 '11 at 4:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.