Use Different Single Templates For Different Categories
You want to use a different single template file for only one category in WordPress. First, create the special single template file. Name it whatever you want. Next, open your theme’s single.php
. At the very top of single.php
, before the head, insert this:
<?php if ( in_category('the-special-cat-slug') ) { include 'special-singlefile.php';} else { ?>
In the code above, you must make change ‘the-special-cat-slug’ in line 1 to your own category slug (the one getting the special template file). Also, change ‘special-singlefile.php’ in line 3 to the name of your special single template file.
Next, at the very end of single.php
, even after the closing html tag, insert this:
<?php } ?>
Variation
This example uses 2 special single template files for 2 categories. Then it calls a 3rd single template file for posts in a 3rd category, but for posts who are also not in some other category (‘cat-slug-4’). Then it calls a 4th single template file for posts that are tagged with tag id #56.
<?php if ( in_category('cat-slug-1') ) { include 'special-singlefile1.php';} elseif ( in_category('cat-slug-2')) { include 'special-singlefile2.php';} elseif ( in_category('cat-slug-3') && !in_category('cat-slug-4')) { include 'special-singlefile3.php';} elseif (has_tag('56')) { include 'special-singlefile4.php'; } else { ?>
Don’t forget the closing curly brace at the very bottom of single.php
:
<?php } ?>
See more: WordPress categories
Questions and Comments are Welcome