0

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}?

1
  • Hi Chris, please be sure check out the editting help page for help with formatting your questions.
    – t31os
    Commented Apr 3, 2011 at 23:06

1 Answer 1

1

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.

1
  • Perfect! I knew it was something like this but couldn't quite get it worked out. Many thanks! Commented Apr 4, 2011 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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