This is shows the Wordpress logo and a single.php image.

WordPress: Using multiple single.php files for different categories.

October 24, 2016

1. Duplicate single.php file.

The first thing we do is duplicate our single.php file and rename the file. For the purpose of this tutorial we have duplicated our single.php file 3 times and named them; template1.php, template2.php, template3.php.

2. Create Relative Categories

The next step is to create your categories. For the purpose of this tutorial we have named the categories; categoryname1 & categoryname2.

3. Pull through the different single.php templates

Now is where the magic happens, remove all code from your single.php file and insert the following snippet:

This code snippet is quite self explanatory but if you are unsure on how it works then here is a breakdown:

– The user creates a post.
– If the category name: ‘categoryname1‘ is selected then load template1.php.
– Else the category name: ‘categoryname2‘ is selected then load template2.php.
– Anything else then load template2.php.

4. Add category when creating a new post.

This part is simple. When creating a new post you simply choose the relevant category and the corresponding template.php file will be displayed.

Share this post: