X
X

Custom single template for a specific category

Link

function get_custom_cat_template($single_template) {
   global $post;
   if ( in_category( 'movies' )) {
      $single_template = dirname( __FILE__ ) . '/single-movies.php';
   }
   return $single_template;
} 
add_filter( "single_template", "get_custom_cat_template" ) ;
add_filter( 'single_template', function ( $single_template ) {

    $parent     = '21'; //Change to your category ID
    $categories = get_categories( 'child_of=' . $parent );
    $cat_names  = wp_list_pluck( $categories, 'name' );

    if ( has_category( 'movies' ) || has_category( $cat_names ) ) {
        $single_template = dirname( __FILE__ ) . '/single-movies.php';
    }
    return $single_template;
     
}, PHP_INT_MAX, 2 );