Set Custom post type template

PHOTO EMBED

Thu Jul 06 2023 11:26:20 GMT+0000 (Coordinated Universal Time)

Saved by @gshailesh27

//set single template for custom post type texonomy

function get_custom_single_template($single_template) {
    global $post;

    if ($post->post_type == 'resources') {
        $terms = get_the_terms($post->ID, 'resources-category');
        if($terms && !is_wp_error( $terms )) {
            //Make a foreach because $terms is an array but it supposed to be only one term
            foreach($terms as $term){
                $single_template = dirname( __FILE__ ) . '/single-'.$term->slug.'.php';
            }
        }
     }
     return $single_template;
}

add_filter( "single_template", "get_custom_single_template" ) ;

content_copyCOPY