WordPress: Different single.php Template by Category
How to use a different single.php template for each WordPress category: the single_template filter in a child theme, in_category() and post templates.
https://www.electronics-engineering.com/wordpress-more-then-one-post-single-php-based-on-category

WordPress uses the same single.php template for every post. Sometimes a group of posts needs a different layout: a category of products, of video channels or of events, with extra fields, scripts or a different sidebar. This article shows how to choose the single post template by category.
You will learn the approach used on this site in 2018, why it is fragile today, the modern way to do it with the single_template filter in a child theme, and how to let editors pick a template post by post.
The original approach (2018)
Inside the theme (better: inside a child theme) the content of single.php was replaced with a small dispatcher:
$post = $wp_query->post;
if ( in_category('238') ) {include(TEMPLATEPATH . '-child/single_tv_channel.php');}
else {include(TEMPLATEPATH . '-child/single_posts.php');}238 is the ID of the category whose posts need the special layout. The file single_posts.php is the old single.php, renamed, and single_tv_channel.php is a new template with its own PHP code (and jQuery), used only for posts in category 238.
Why this code is fragile today
TEMPLATEPATHis the folder of the parent theme. Appending-childworks only if the child theme folder is named exactly like the parent plus-child. The constant is also deprecated since WordPress 6.4: the functions areget_template_directory()for the parent andget_stylesheet_directory()for the active (child) theme.- A numeric category ID differs between a local copy and the live site if the category was created separately; a slug is easier to read and to move.
- Replacing
single.phpwith a dispatcher hides the real template from the template hierarchy, from plugins that filter templates and from anyone who reads the theme later.
The modern approach: the single_template filter
WordPress chooses the template through the template hierarchy and then passes the chosen file through a filter, {$type}_template, which for single posts is single_template. Leave single.php untouched and add this to the functions.php of the child theme:
<?php
// functions.php of the child theme
add_filter( 'single_template', function ( $template ) {
if ( in_category( 'tv-channels' ) ) {
$custom = locate_template( 'single-tv-channel.php' );
if ( $custom ) {
return $custom;
}
}
return $template;
} );single-tv-channel.php is a copy of single.php placed in the child theme and adapted. locate_template() looks for it in the child theme first and then in the parent, so the code does not depend on folder names, and if the file is missing WordPress falls back to the normal template.
Details worth knowing
in_category()accepts an ID, a slug, a name or an array of them:in_category( array( 'tv-channels', 'radio' ) )matches either category.- It checks only the categories assigned to the post, not their parents: a post in a subcategory of
tv-channelsdoes not match unless you also test the children (for example withget_term_children()). - Scripts needed only by the special template (the jQuery code of the original example) should be loaded with
wp_enqueue_script()inside awp_enqueue_scriptscallback that checks the same condition, not printed inside the template.
Alternative: let the editor choose the template
If the choice is not tied to a category, a classic theme can offer templates for posts, not only for pages (WordPress 4.7 and later). A file with this header appears in the Template menu of the post editor:
<?php
/*
* Template Name: TV channel
* Template Post Type: post
*/In a block theme the same result is reached without code: create a custom template in the Site Editor and assign it from the Template panel of the post.
Summary
- To use a different single post template for a category, filter
single_templatein the child theme and return a file found withlocate_template(). - Avoid the old
TEMPLATEPATH . '-child'trick: the constant is deprecated since WordPress 6.4 and the path depends on folder names. - Prefer category slugs to numeric IDs, and remember that
in_category()ignores parent categories. - For a per-post choice, use the
Template Post Typeheader or a custom template in the Site Editor.






Welcome to the page on “WordPress: More Then One Post (Single.php) Based on Category”. I wrote it for engineering students: if a step does not add up, an exercise gives you a different result or you would like more material on the topic, just ask in the comments and I will reply. A star rating above helps me understand which pages to improve.