Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More

Different Templates For WordPress Category Posts

Last updated on:

When I first set up WPExplorer.com I wanted to have different template designs for my premium themes, free themes, plugins and blog pages. Back then there weren’t any custom post types and custom taxonomies seemed a bit too much, especially since I wanted to keep all the posts within the root folder, so instead I found the code below which allows you to create custom template designs for your single posts based on their categories.

Style Posts From WordPress Categories Differently

Follow the 2 simple steps below so you can create different styles for your single posts for each category.

Step 1: Insert Into Your Functions.php File

Copy and paste the following code into your functions.php file. It creates a new function that assigns a template to your category if a file named single-categoryID.php exits in your theme’s folder.

// add single types
add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") ) return TEMPLATEPATH . "/single-{$cat->term_id}.php"; } return $t;' ));

Step 2: Create Your “Templates”

Now you can easily create a template for each of your categories by adding new files to your theme using the following naming structure (simple copy your single.php file, rename and change “categoryid” with your corresponding category id and then style accordingly):

single-CATEGORYID.php
Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

Comments

No comments yet. Why don't you kick off the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn how your comment data is processed by viewing our privacy policy here.