Assign single template to multiple custom post types?

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?

Read More

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?

Related posts

1 comment

  1. 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'; 
    });
    

Comments are closed.