4

I have a number of custom post types that I would like to display with the same template which I have created initially as single-photography.php in my theme.

Is there any way to assign this template to multiple custom post types?

I understand I can probably do this within the single.php file with a conditional include based on get_post_type(), but I'm wondering if there is a better way?

7

You can filter template_include and pass your custom template as return value depending on your own conditions.

Sample code, not tested:

add_filter( 'template_include', function( $template ) 
{
    // your custom post types
    $my_types = array( 'photography', 'painting' );
    $post_type = get_post_type();

    if ( ! in_array( $post_type, $my_types ) )
        return $template;

    return get_stylesheet_directory() . '/single-photography.php'; 
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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