Archived topic
Custom layout for post in specific category
1 reply · Started by tobymiguel on September 22, 2018
Is it possible to create a file like single.php for a post in a specific category?
Background: I have some posts in a specific category with a highly customized layout where comments will be displayed in a different place etc. Therefore I want to create a completely different single.php and not just content-single.php for posts in that category. Is this possible with single-category-name.php for example?
Hi there,
This looks like the best solution: https://wordpress.stackexchange.com/a/168763
For example:
add_filter( 'single_template', function( $single_template ) {
if ( in_category( 1 ) ) {
$file = get_stylesheet_directory() . '/single-cat-1.php';
if ( file_exists($file) ) {
return $file;
}
}
return $single_template;
} );
You'd have to update the 1 to the ID of your category, and single-cat-1.php to the name of your file, which would have to be in your child theme.